Home arrow PHP arrow Page 4 - Parsing Child Nodes with the DOM XML extension in PHP 5

The removeChild() method - PHP

In this last chapter of the series, I’m going to teach you how to handle the child nodes of an XML document by way of two simple methods, called hasChildNode() and removeChild() respectively. So let’s not waste any more time in preliminaries and learn how to use them in a helpful way.

  1. Parsing Child Nodes with the DOM XML extension in PHP 5
  2. Reintroducing a few methods of the DOM XML extension
  3. Working with the hasChildNodes() method
  4. The removeChild() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
April 08, 2008

print this article



If you’ve ever used the DOM API with JavaScript to dynamically remove several elements of a web page, then you’ll find the “removeChild()” method of the DOM XML extension very easy to grasp, since its functionality is identical to that of the one provided by its client-side counter part.

So, to do things a bit easier and faster, I’m going to use the same “headlines.xml” file that you saw earlier to exemplify how the “removeChild()” method works. That being said, here’s the familiar signature of this basic XML file:

// definition of 'headlines.xml' file

<?xml version="1.0" encoding="iso-8859-1"?>


<headline id="economics">


<url>Link for headline 1 goes here</url>

<text>Text for headline 1 goes here</text>


<headline id="sports">


<url>Link for headline 2 goes here</url>

<text>Text for headline 2 goes here</text>


<headline id="jetset">


<url>Link for headline 3 goes here</url>

<text>Text for headline 3 goes here</text>


<headline id="technology">


<url>Link for headline 4 goes here</url>

<text>Text for headline 4 goes here</text>


<headline id="art">


<url>Link for headline 5 goes here</url>

<text>Text for headline 5 goes here</text>



And here’s a simple script that uses the “removeChild()” method to delete the entirety of the <image> elements contained by the parent <headline> nodes.

The corresponding code sample looks like this:

// example on removing child nodes using the 'removeChild()' method

$dom=new DOMDocument;

// load XML data from existing file


// get <headline> nodes


foreach($headlines as $headline){


// remove child node




Do you realize how easy it is to delete child nodes from an XML document? I guess you do! As you saw, in the above hands-on example, I used a combination of the “removeChild()” method and the brand new “childNodes” object in order to navigate to the first item of each “<headline> node and delete it.

Of course, this is merely a basic demonstration of how to implement the “removeChild()” method in a useful manner. But you’re completely free to develop your own testing examples regarding the use of this one and the rest of the methods discussed in this article.

Final thoughts

It’s hard to believe, but we’ve come to the end of this series. In general terms, the experience has been instructive and also fun, since you learned how to use some of the most relevant methods that come packaged with the DOM XML PHP extension.

As I stated at the beginning, this is only a brief guide on what you can do with the DOM API when working with XML documents. If you’re looking for a full reference guide on this library, the best place to go is the official PHP website.

See you in the next PHP web development tutorial!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: