Before you start learning a brand new pair of methods aimed at handling the child nodes of a specified XML document, first I’d like to reintroduce some methods that were discussed in the last tutorial of the series. This way, you can fill some gaps regarding their concrete utilization. Below I included two basic hands-on examples that demonstrate how to use the “getAttribute()” and “has Attribute()” methods in order to work with the respective attributes of a sample “headlines.xml” file: Here are the corresponding code samples, along with the definition of the mentioned XML file. So take a close look at them, please: // 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> // example on using the 'getAttribute()' method $dom=new DOMDocument(); $dom->load('headlines.xml'); $headlines=$dom->getElementsByTagName('headline'); foreach($headlines as $headline){ 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 */ // 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 by the previous examples, checking for the existence of a particular attribute that belongs to a specific XML node and getting its corresponding value is actually a no-brainer process that can be accomplished with minor problems by using the “hasAttribute()” and “getAttribute()” methods shown above. In this case, the methods in question are used to parse the node attributes of a trivial XML file, but of course, the same business logic can be applied when working with more complex XML data. With the two earlier code examples well underway, this is an appropriate moment to continue exploring some other helpful features offered by the DOM XML extension. So in accordance with the concepts that I deployed in the introduction, in the next section of this article, I’m going to show you how to determine whether or not a specified node within an XML document has child nodes. As you might have guessed, to see the complete details about how this will be achieved, simply click on the linked title that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|