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

Reintroducing a few methods of the DOM XML extension - 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

Before you start learning a brand new pair of methods aimed at handling the child nodes of a specified XML document, first I’d like to reintroduce some methods that were discussed in the last tutorial of the series. This way, you can fill some gaps regarding their concrete utilization.

Below I included two basic hands-on examples that demonstrate how to use the “getAttribute()” and “has Attribute()” methods in order to work with the respective attributes of a sample “headlines.xml” file:

Here are the corresponding code samples, along with the definition of the mentioned XML file. So take a close look at them, please:


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


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


$dom=new DOMDocument();

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

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

foreach($headlines as $headline){

echo 'ID attribute of current node is the following: '.$headline->getAttribute('id').'<br />';

}


/* displays the following

ID attribute of current node is the following: economics

ID attribute of current node is the following: sports

ID attribute of current node is the following: jetset

ID attribute of current node is the following: technology

ID attribute of current node is the following: art

*/ 


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

$dom=new DOMDocument();

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

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

foreach($headlines as $headline){

if($headline->hasAttribute('id')){

echo 'ID attribute of current node is the following: '.$headline->getAttribute('id').'<br />';

}

}


/* displays the following

ID attribute of current node is the following: economics

ID attribute of current node is the following: sports

ID attribute of current node is the following: jetset

ID attribute of current node is the following: technology

ID attribute of current node is the following: art

*/


As demonstrated by the previous examples, checking for the existence of a particular attribute that belongs to a specific XML node and getting its corresponding value is actually a no-brainer process that can be accomplished with minor problems by using the “hasAttribute()” and “getAttribute()” methods shown above.

In this case, the methods in question are used to parse the node attributes of a trivial XML file, but of course, the same business logic can be applied when working with more complex XML data.

With the two earlier code examples well underway, this is an appropriate moment to continue exploring some other helpful features offered by the DOM XML extension. So in accordance with the concepts that I deployed in the introduction, in the next section of this article, I’m going to show you how to determine whether or not a specified node within an XML document has child nodes.

As you might have guessed, to see the complete details about how this will be achieved, simply click on the linked title that appears below and keep reading.



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