Using removeChild with the PHP 5 DOM
4 May 2005, by Karl Bunyan
The documentation for PHP 5's DOM functions isn't at its most helpful yet, so I thought an example of how to use 'removeChild' wouldn't go amiss.
The documentation for PHP 5’s DOM functions isn’t at its most helpful yet, so I thought an example of how to use ‘removeChild’ wouldn’t go amiss.
Assuming first that you have some DOMDocument XML in a variable called $xml, that may look something like this:
<root>
<labels>
<title>My title</title>
</labels>
</root>
The first thing to do is to get a handle on the node you want to remove. We’re going to remove the node named ‘title’:
$node=$xml->getElementsByTagName("title")->item(0);
Then, simply remove $node from its parent:
$xml->getElementsByTagName("labels")->item(0)->removeChild($node);
Not too much to it at all.
PHP DOM removeChild documentation
May 4, 2005 at 10:33 am
Filed in: PHP, Programming
No comments