As I said at the beginning, the DOM XML PHP library offers a number of methods that can be used to access attributes of one or more nodes within an XML document. The first of these attribute-related methods that I’m going to show you is named “getAttribute().” It is similar to its client-side incarnation and allows you to retrieve the attribute of a specified element. To understand more clearly how this method functions, first I’m going to define a trivial XML file that contains data about some fictional headlines. The structure looks like this: // 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> As you can see, the previous “headlines.xml” file is pretty basic; it’s only composed of a few simple nodes that store information about some sample headlines, including the corresponding titles, thumbnails, and links. Besides, each headline has an ID attribute that helps to differentiate the different categories, such as “sport,” “economics,” “art,” etc. So far, this shouldn't be too complex to grasp. Now, with the previous XML file already created, take a look at the example below. It demonstrates how to use the aforementioned “getAttribute()” method to retrieve the respective values of the ID attributes assigned to each document node. Here’s the corresponding code sample: // 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 */ See how easy it is to retrieve the attribute values of certain nodes within an XML document? I bet you do! In this case, I first used the handy “getElementsByTagName()” method to get access to all of the <headline> elements of the document. I then obtained their ID values via the “getAttribute()” method. Short and simple! At this point, I’m pretty sure that you've grasped the logic that drives the “getAttribute()” method. Thus, it’s time to learn more features of the DOM XML extension. In the next section I'm going to show you how to use a brand new method that belongs to this PHP library. It's called “has Attribute()” and can be used to determine whether or not a specific XML node has attributes. To see the full details of how this brand new method will be implemented, please visit the following section. It’s only one click away.
blog comments powered by Disqus |
|
|
|
|
|
|
|