Home arrow PHP arrow Page 2 - Working with Multiple Document Nodes with the DOM XML Extension in PHP 5

Getting a collection of XML nodes - PHP

Welcome to the fourth article of the series "A quick overview of the DOM XML extension in PHP 5." By means of easy-to-grasp hands-on examples, this series equips you with a friendly guide to using the most relevant methods that come bundled with the DOM XML PHP extension to help you work with XML documents in a truly painless way.

  1. Working with Multiple Document Nodes with the DOM XML Extension in PHP 5
  2. Getting a collection of XML nodes
  3. Loading XML data from a specified text file with the load() method
  4. Reading data from a specified XML string with the loadXML() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 18, 2008

print this article



During the course of the prior tutorial of the series, you saw how to use the “getElementById()” method included in DOM XML extension to access specific nodes within an XML document by its ID attribute. In a similar manner, the extension also provides the “getElementsByTagName()” method, which as its name suggests, can be used to retrieve a collection of nodes that share the same tag name.

If you ever used the client-side version of this method with JavaScript, then surely you’ll find it very easy to learn and use. Still, it's better that I show you a concrete example of how to utilize it, so you can quickly grasp how it works.

Please take a look at the following code sample, aimed at demonstrating the functionality of the aforementioned “getElementsByTagName()” method:

// example on creating a new DOMDocument object and using the 'getElementsByTagName()' method to extract nodes with a specific tag name

$elements=array('element1'=>'Element 1','element2'=>'Element 2','element3'=>'Element 3','element4'=>'Element 4','element5'=>'Element 5','element6'=>'Element 6','element7'=>'Element 7','element8'=>'Element 8','element9'=>'Element 9','element10'=>'Element 10');

$dom=new DOMDocument('1.0','iso-8859-1');


// insert the root element into the document


// insert additional elements into the document

foreach($elements as $key=>$value){


// append element to document



// display nodes with 'testnode' name

foreach($dom->getElementsByTagName('testnode') as $node){

echo $node->tagName.'<br />';


/* displays the following












As you can see, the above PHP 5 script first creates a simple XML document by using a PHP array, and then inserts a few basic nodes into it. Finally, it displays the entire contents on the browser. So far, nothing unexpected happens, right? However, notice how in the end, the pertinent “getElementsByTagName()” method is used to extract all the nodes that have a <testnode> tag name, and finally iterate over all of them.

In this case, it’s clear to see the similarity between the previous method and the one used to access multiple elements of a web document via the DOM. Of course, here I’m using the “getElementsByTagName()” method to retrieve multiple nodes within an XML document in the server, but surely you’ll agree that this process is nearly identical to its client-side version.

All right, at this point you learned how to get collections of XML nodes that have the same tag name, thanks to a simple example. The question is, what’s the next method of the DOM XML extension that needs to be discussed in detail? Well, since in the introduction of this article I promised to teach you how to process multiple XML nodes, in the following section, I’ll be examining a brand new method. It's called “load()” and it will be useful for loading XML data from a remote file.

To see how the method in question works, click on the link below and start reading.

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