我有以下XML
<cds>
<record>
<id>1</id>
<artist>Rammstein</artist>
<album>random</album>
<trackNumbers>11</trackNumbers>
</record>
<record>
<id>2</id>
<artist>Rammstein</artist>
<album>random</album>
<trackNumbers>18</trackNumbers>
</record>
</cds>
我想删除从另一个PHP文件传递的标识符“ ID”记录。因此,如果我没记错的话,我需要记录节点的位置以删除该节点。
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
unset($xml->record[x]); // x should be the id passed
这可以实现吗?我一直在尝试获取此信息,但无法找到解决方案。
首先选择<record>
有xpath()
,然后将其删除:
$xml = simplexml_load_string($x); // assume XML in $x
$record = $xml->xpath("/cds/record[id='2']")[0];
这将在中存储该xpath“查询”的第一个结果(索引= 0)$record
。这将是一个ID = 2的记录节点。请注意,进行数组解引用需要PHP> = 5.4。
现在使用unset:
unset($record[0]);
查看更改:
echo $xml->asXML();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句