Reputation: 2975
I have a xml which the format is like this:
<root>
<a>1</a>
<b>2</b>
<c></c>
</root>
This is the code I have tried:
$to = 3;
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->load("../xxx.xml");
$xpath = new DOMXPath($dom);
$query = "/root/*[position()=$to]";
$nodes = $xpath->query($query);
$node = $nodes[0];
$dom->removeChild($node);
$dom->save("../xxx.xml", LIBXML_NOEMPTYTAG);
How can I delete the tag with name "c" ?
Upvotes: 1
Views: 803
Reputation: 2975
Oh lord, the problem was lie under
$dom->removeChild($node);
should be
$node->parentNode->removeChild($node);
in order to delete a node, you have to get back to the parent node and then it will take the action..I think, this is just my two cents. if someone understand well, feel free to correct me
Upvotes: 1