Home arrow PHP arrow Page 4 - Web Services: SimpleXML

Parsing the XML - PHP

In this third part of a five-part series on Web Services, we'll wrap up our discussion of MagpieRSS and move on to SimpleXML, an intuitive methodology for processing XML structures. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Web Services: SimpleXML
  2. SimpleXML
  3. Loading XML
  4. Parsing the XML
By: Apress Publishing
Rating: starstarstarstarstar / 1
August 05, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
   
$xml = simplexml_load_file("books.xml");
   
foreach($xml->book as $book) {
       
printf("%s is %s. <br />",$book->author, $book->author->attributes());
   
}
?>

This example returns the following:

--------------------------------------------
Jane Austen is female.
Alberto Moravia is male.
Ernest Hemingway is male.
--------------------------------------------

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:

--------------------------------------------
male
--------------------------------------------

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) {
    printf("%s = %s <br />", $a, $b);
}

This example returns the following:

--------------------------------------------
gender = female
age = 20
--------------------------------------------

Please check back next week for the continuation of this article.



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: