Jump to content

remove child node


harshpandya

Recommended Posts

Here is my XML doc::<root><device> <name>My name</name></device></root>Here is my code : I want to remove name node from the XML and create new one with different node value$root = $xmlDoc->documentElement;$child2 = $root->getElementsByTagName("name")->item(0);$root->removeChild($child2);why this wont work?Thanks,

Link to comment
Share on other sites

I believe you can only remove a child once it is a child. In your case, $child2 is a descendant (grandchild if you will) of root, not a child.But if you're going to create a new name with a value, why not just edit the value of the current name? This would be more efficient, and simple:

$xmlDoc->getElementsByTagName("name")->item(0)->nodeValue = 'The new value of the first name element';

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...