In the course of the previous section, I mentioned that the DOM XML library provides PHP developers with a useful method, called “has Attribute()”, which can be utilized to find out if a specified node of an XML document includes an attribute. Obviously, as you’ll see in a few moments, the implementation of this method is very straightforward. Therefore, I'm going to use the same “headlines.xml” file that you saw in the prior section to demonstrate how to use this method. Given that, here’s the corresponding definition of this sample “headlines.xml” file: // 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, please pay close attention to the following PHP 5 script. It uses the previously-mentioned “has Attribute()” method to determine if each <headline> node of the above “headlines.xml” file contains an attribute or not. If it has one, its value is outputted to the browser: // 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 above, once the contents of the “headlines.xml” file have been read via the already familiar “load()” method, they’re parsed with a simple “foreach” PHP loop. Naturally, the most relevant thing to stress here is how the “has Attribute()” method is used to determine if each <headline> node contained in the file has an ID attribute. If this last condition is true, then the respective ID's values are simply echoed to the browser. Now don’t say that this method wasn’t simple to grasp! So far, everything looks good. At this time you should feel pretty satisfied, since you learned how to use a couple of methods included with the DOM XML extension to find out whether certain nodes of a given XML document have attributes, as well as to retrieve the values of the attributes in question. Nevertheless, this educational journey exploring the main features of the DOM XML extension isn't finished yet. There are other useful methods that deserve a closer analysis. Therefore, the last section of this tutorial will be focused on the library’s capability to clone the nodes of a specified XML document. Sounds really interesting, right? But, to learn how to work with clone nodes, you’ll have to click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|