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.
blog comments powered by Disqus |
|
|
|
|
|
|
|