Using removeChild with the PHP 5 DOM

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:


My title

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

Advertisements
This entry was posted in PHP, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s