Working with Multiple Document Nodes with the DOM XML Extension in PHP 5

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.

If you’re a PHP developer who builds web applications that work with XML documents on a frequent basis, then there’s good news for you. This powerful server-side scripting language comes equipped with an extension, DOM XML, which permits you to handle XML data directly through the API provided by the Object Document Model.

Naturally, as with everything in life, learning the principal features offered by this XML-related PHP extension takes a while, but you can drive on the fast lane and save much of your time an effort by reading this group of articles right now!

At this point, having introduced the main subject of this article series, I’d like to spend a little time recapitulating the items that were discussed in the previous tutorial, just in case you didn’t have a chance to read it.

In that installment I explained how to use a couple of helpful methods included with the DOM XML extension, named “createComment()” and “createCDATASection()” respectively, which not surprisingly came in handy for inserting comments and CDATA nodes into an existing XML document.

Since appending comments and CDATA sections to a particular XML document tree was accomplished by using the DOM API, the whole process was simple, meaning that you shouldn’t have major problems understanding how these methods do their respective things.

So far, so good. At this stage, I quickly reviewed the topics treated in the previous article of the series, so it’s time to learn a few other methods included with the DOM XML extension. Based upon this premise, in this fourth tutorial I’m going to show you how to get access to multiple nodes of an XML document, either for internal processing or simply for echoing to the browser.

As you may have guessed, these multi-node operations require working with brand new methods of the DOM XML library, so let’s not waste more time in preliminaries. 

{mospagebreak title=Getting a collection of XML nodes}

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′);

$rootElement=$dom->createElement(‘rootnode’,”);

// insert the root element into the document

$dom->appendChild($rootElement);

// insert additional elements into the document

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

$element=$dom->createElement(‘testnode’,$value);

// append element to document

$rootElement->appendChild($element);

}

// display nodes with ‘testnode’ name

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

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

}


/* displays the following

testnode

testnode

testnode

testnode

testnode

testnode

testnode

testnode

testnode

testnode

*/


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.

{mospagebreak title=Loading XML data from a specified text file with the load() method}

As I stated in the previous section, the DOM XML extension comes equipped with some helpful methods for moving entire XML strings from a text file to a variable and vice versa. Based on this concept, I’m going to show you how to use the “load()” method, whose function is basically loading the contents of a specified XML file onto the web server’s memory.

Having explained briefly how this method works, suppose that there’s an XML file that contains data about some hypothetical headlines, whose structure looks like this:

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

Indeed, the above XML file is very understandable, since it’s been populated with a few nodes which store data about the mentioned headlines, including their respective titles, thumbnails, and links as well. In addition, each headline has an ID attribute that helps to separate them into different categories. This is pretty simple to follow, right?

Now that you have seen how the previous XML file looks, please examine the code of the following hands-on example, which shows how to read the contents of this file by using the handy “load()” method:

// example on loading XML data from an existing file using the ‘load()’ method


$dom=new DOMDocument();

// load XML data from file

$dom->load(‘headlines.xml’);

// tell the browser the output is XML via the ‘Content-Type’ HTTP header

header(‘Content-Type: text/xml’);

// display DOM document

echo $dom->saveXML();


/* displays the following

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

*/

As you can see, reading the contents of a specified XML file with the “load()” method is a straightforward process that can be performed with minor hassles. In this case, the XML data is transferred directly from the source file to the web server’s memory, and then echoed to the browser via the “saveXML()” method that you already learned in previous articles of the series.

Okay, at this point, I’ve taught you how to read the contents of a simple XML file by means of the “load()” method included with the DOM XML extension. Yet this library features plenty of methods for loading XML data from remote sources. For example, there is “loadXML()”, which has the ability to read XML contents from a specific string.

Therefore, assuming that you’re interested in learning how to use this method, in the last section of this article I’m going to provide you with a practical example that will show it in action.

Of course, this particular topic will be covered in detail in the next few paragraphs, so please click on the link that appears below and keep reading.

{mospagebreak title=Reading data from a specified XML string with the loadXML() method}

As I noted in the previous section, the DOM XML extension offers a specific method whose function is based on reading XML data straight from a given string. The method is called “loadXML()”, and an example of how to use is listed below, so take a look at it, please:

// example on loading XML data from an existing string using the ‘loadXML()’ method

$xml='<?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></headlines>';

$dom=new DOMDocument();

// load XML data from string

$dom->loadXML($xml);

// tell the browser the output is XML via the ‘Content-Type’ HTTP header

header(‘Content-Type: text/xml’);

// display DOM document

echo $dom->saveXML();


/* displays the following

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

</headlines>

*/

Definitely, reading XML data from a specified string by using the previous “loadXML()” method is not only simple, but pretty fun, don’t you think? At the very least, you’ll have to agree with me that the DOM XML extension makes reading XML data either from a specific file or from a plain string a no-brainer process.

Here’s my final suggestion: feel free to tweak all of the code samples included here to acquire a better understanding of working with the DOM XML PHP extension. Have fun!

Final thoughts

As I said before, we’ve come to a close. However, hopefully this fourth chapter of the series has opened your eyes a bit more about the use of the methods reviewed earlier. As I showed you, they can be used to handle collections of XML nodes, and even complete XML files and strings.

In the next part of the series, I’m going to show you how to utilize the DOM XML library to read plain HTML files, as well as build HTML documents by using their respective DOM-based representation.

Now that you’re aware of the topics that will be discussed in the upcoming article, you won’t want to miss it!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan