[英]Delete Xml data using php
How can I delete specific elements on xml using php 如何使用php删除xml上的特定元素
my.xml my.xml
<usr>
<uid trk= "1234">
<deleteThis>
<mychild>here</mychild>
<anotherchild>here</anotherchild>
</deleteThis>
</uid>
</usr>
I want to remove the "deleteThis" element and its children 我想删除“deleteThis”元素及其子元素
result: 结果:
<usr>
<uid trk= "1234">
</uid>
</usr>
here's my non-working code 这是我的无效代码
index.php 的index.php
$xml = new DOMDocument;
$xml->load('my.xml');
$thedocument = $xml->documentElement;
$list = $thedocument->getElementsByTagName('uid');
foreach ($list as $domElement){
$attrValue = $domElement->getAttribute('trk');
if ($attrValue == "1234") { //if <uid trk= "1234">
$valY = $domElement->getElementsByTagName('deleteThis');
$thedocument->removeChild($valY);
}
}
$xml->save("my.xml");
It seems it doesn't found the node. 似乎找不到该节点。
if ($attrValue == "1234") { //if <uid trk= "1234">
$valY = $domElement->getElementsByTagName('deleteThis');
//$valY is a DOMNodeList, that you happen to know there is only one doesnt matter
foreach($valY as $delnode){
$delnode->parentNode->removeChild( $delnode);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.