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

Working with the hasChildNodes() 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

So far, you learned how to use the functionality provided by the DOM XML extension to iterate over a certain number of nodes contained within a simple XML document and retrieve their respective attributes. Nonetheless, as I suggested before, the library has a few additional methods that permit you to work directly with the child nodes of a specified element, as you’ve probably done hundreds of times, when using JavaScript to manipulate web documents on the client-side.

Speaking more specifically, the first method that I’m going to show you, with regard to handling child nodes, is one called “hasChildNodes().” It is tasked with determining whether a concrete node of an XML document also contains sub nodes (or child elements).

To illustrate how the “hasChildNodes()” does its thing, first I’m going to include the definition of the sample “headlines.xml” file that you saw in the previous section. Here it is:


// definition of 'headlines.xml' file


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

<headlines>

<headline id="economics">

<image>image1.jpg</image>

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

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

</headline>

<headline id="sports">

<image>image2.jpg</image>

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

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

</headline>

<headline id="jetset">

<image>image3.jpg</image>

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

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

</headline>

<headline id="technology">

<image>image4.jpg</image>

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

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

</headline>

<headline id="art">

<image>image5.jpg</image>

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

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

</headline>

</headlines>


Now that you've recalled how the above XML file looks, let me go one step further and show you a short script that traverses the file in question and checks to see if each of its <headline> elements has a child node or not.

The signature of this brand new script is the following:


// example on using the 'hasChildNodes()' method


$dom=new DOMDocument();

// load XML data from existing file

$dom->load('headlines.xml');

// get <headline> nodes

$headlines=$dom->getElementsByTagName('headline');

foreach($headlines as $headline){

if($headline->hasChildNodes()){

echo 'This node has children!<br />';

}

}


/* displays the following

This node has children!

This node has children!

This node has children!

This node has children!

This node has children!

*/


Despite the simplicity of the above code sample, I’m reasonably sure that it’s been useful enough to demonstrate the logic that drives the “hasChildNodes()” method. On this specific occasion, since each <headline> element within the pertinent XML document does wrap a group of child nodes, this condition is reflected by echoing an indicative message on the browser. Grasping how this method does its thing definitely isn’t rocket science.

Well, at this point you should congratulate yourself, since you learned one more method in the vast arsenal provided by the DOM XML extension. Nonetheless, if you're used to working with XML documents on a frequent basis, then it’s highly possible that you’re wondering if this extension has the capability to remove an element’s child node. Luckily, the answer is an emphatic yes! And this will be the last topic that I’m going to discuss in this tutorial.

Deleting a child node from a given XML document is a process that must be performed by way of yet another method of the DOM XML extension, whose name is “removeChild().”

To see how it will be implemented in the context of a concrete example, please click on the link below and keep reading. We’re almost done!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: