PHP SimpleXML
SimpleXML functions and methods reference.
Key PointsSimpleXML objects are not plain objects — cast to `(string)` before use.
Functions
| Function | Purpose |
|---|---|
| simplexml_load_string | 从字符串加载 |
| simplexml_load_file | 从文件/URL 加载 |
| simplexml_import_dom | DOM 节点转 SimpleXML |
Methods
| Method | Purpose |
|---|---|
| children | 取子元素(可指定命名空间) |
| attributes | 取属性 |
| xpath | XPath 查询 |
| asXML | 导出 XML 字符串 |
| getName | 元素名 |
| getNamespaces | 所有命名空间 |
| addChild | 加子元素 |
| addAttribute | 加属性 |
PHP
<?php
$xml = '<catalog>
<item sku="A1"><name>键盘</name><price>199</price></item>
<item sku="A2"><name>鼠标</name><price>89</price></item>
</catalog>';
$s = simplexml_load_string($xml);
echo count($s->item), "\n";
echo $s->item[0]["sku"], "\n";
echo (string)$s->item[1]->name, "\n";
$total = 0;
foreach ($s->item as $it) {
$total += (int)$it->price;
}
echo $total, "\n";Output
2
A1
鼠标
288XPath and attributes
PHP
<?php
$s = simplexml_load_string($xml);
$r = $s->xpath('//item[@sku="A2"]/name');
echo (string)$r[0], "\n";
$r = $s->xpath('//item[price > 100]/@sku');
echo implode(",", array_map("strval", $r)), "\n";
// 属性的两种访问
echo $s->item[0]["sku"], "\n";
echo $s->item[0]->attributes()["sku"], "\n";
echo $s->item[0]->attributes()->count(), "\n";Output
鼠标
A1
A1
A1
1Export and modify
PHP
<?php
$s = simplexml_load_string($xml);
$s->item[0]->price = "179";
$s->item[0]->addChild("stock", "50");
$s->item[1]->addAttribute("sale", "1");
$doc = dom_import_simplexml($s)->ownerDocument;
$doc->formatOutput = true;
echo $doc->saveXML();Output
<?xml version="1.0"?>
<catalog>
<item sku="A1">
<name>键盘</name>
<price>179</price>
<stock>50</stock>
</item>
<item sku="A2" sale="1">
<name>鼠标</name>
<price>89</price>
</item>
</catalog>Assigning creates or overwrites a child. Deleting needs
unset($s->item[0]->price), and the resulting structure can surprise you — use DOM for complex edits.Pitfalls
- **Missing
(string)** —— you get an object and==comparisons misbehave - **
count()** returns 1 or 0, neverfalse - **Empty elements are not
null** —— after casting you get"" - **Namespaced elements need
children()** —— direct access yields empty
if ($s->maybe) is always true because it is an object, even when the element is absent. Use if ((string)$s->maybe !== "") — SimpleXML's most counter-intuitive behaviour.函数
| 函数 | 作用 |
|---|---|
| simplexml_load_string | 从字符串加载 |
| simplexml_load_file | 从文件/URL 加载 |
| simplexml_import_dom | DOM 节点转 SimpleXML |
方法
| 方法 | 作用 |
|---|---|
| children | 取子元素(可指定命名空间) |
| attributes | 取属性 |
| xpath | XPath 查询 |
| asXML | 导出 XML 字符串 |
| getName | 元素名 |
| getNamespaces | 所有命名空间 |
| addChild | 加子元素 |
| addAttribute | 加属性 |
PHP
<?php
$xml = '<catalog>
<item sku="A1"><name>键盘</name><price>199</price></item>
<item sku="A2"><name>鼠标</name><price>89</price></item>
</catalog>';
$s = simplexml_load_string($xml);
echo count($s->item), "\n";
echo $s->item[0]["sku"], "\n";
echo (string)$s->item[1]->name, "\n";
$total = 0;
foreach ($s->item as $it) {
$total += (int)$it->price;
}
echo $total, "\n";Output
2
A1
鼠标
288xpath 与属性
PHP
<?php
$s = simplexml_load_string($xml);
$r = $s->xpath('//item[@sku="A2"]/name');
echo (string)$r[0], "\n";
$r = $s->xpath('//item[price > 100]/@sku');
echo implode(",", array_map("strval", $r)), "\n";
// 属性的两种访问
echo $s->item[0]["sku"], "\n";
echo $s->item[0]->attributes()["sku"], "\n";
echo $s->item[0]->attributes()->count(), "\n";Output
鼠标
A1
A1
A1
1导出与修改
PHP
<?php
$s = simplexml_load_string($xml);
$s->item[0]->price = "179";
$s->item[0]->addChild("stock", "50");
$s->item[1]->addAttribute("sale", "1");
$doc = dom_import_simplexml($s)->ownerDocument;
$doc->formatOutput = true;
echo $doc->saveXML();Output
<?xml version="1.0"?>
<catalog>
<item sku="A1">
<name>键盘</name>
<price>179</price>
<stock>50</stock>
</item>
<item sku="A2" sale="1">
<name>鼠标</name>
<price>89</price>
</item>
</catalog>直接
$s->item[0]->price = "179" 会创建或覆盖该子元素。要删元素得用 unset($s->item[0]->price)——但删完再 asXML() 的结构可能和预期不同,复杂改动还是换 DOM。常见坑
- **取值忘
(string)** —— 得到的是对象,==比较会失效 - **
count()对单元素返回 1**,对零元素返回 0,不是false - **空元素不是
null** ——(string)之后是空字符串 - **命名空间元素要
children()** —— 直接访问拿到空
if ($s->maybe) 永远为真,因为 $s->maybe 是对象(哪怕不存在,也是空对象)。必须 if ((string)$s->maybe !== "")。这是 SimpleXML 最反直觉的地方。