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