Once an XML document has been loaded into an object, several methods are at your disposal. Presently, four methods are available, each of which is introduced in this section. Learning More About an Element XML attributes provide additional information about an XML element. In the sample XML document in the previous Listing 20-4, only theauthornode possesses an attribute, namelygender, used to offer information about the author’s gender. You can use theattributes()method to retrieve these attributes. Its prototype follows: object simplexml_element->attributes() For example, suppose you want to retrieve the gender of each author: <?php This example returns the following: -------------------------------------------- You can also directly reference a particular book author’s gender. For example, suppose you want to determine the gender of the author of the second book in the XML document: echo $xml->book[2]->author->attributes(); This example returns the following: -------------------------------------------- Often a node possesses more than one attribute. For example, suppose theauthornode looks like this: <author gender="female" age="20">Jane Austen</author> It’s easy to output the attributes with aforloop: foreach($xml->book[0]->author->attributes() AS $a => $b) { This example returns the following: -------------------------------------------- Please check back next week for the continuation of this article.
blog comments powered by Disqus |
|
|
|
|
|
|
|