Recipe 12.3. Parsing Basic XML Documents


12.3.1. Problem

You want to parse a basic XML document that follows a known schema, and you don't need access to more esoteric XML features, such as processing instructions.

12.3.2. Solution

Use the SimpleXML extension. Here's how to read XML from a file:

<?php $sx = simplexml_load_file('address-book.xml'); foreach ($sx->person as $person) {     $firstname_text_value = $person->firstname;     $lastname_text_value = $person->lastname;     print "$firstname_text_value $lastname_text_value\n"; } ?> David Sklar Adam Trachtenberg

12.3.3. Discussion

SimpleXML has been described as "the mostest bestest thing ever." While it's hard to live up to such grand praise, SimpleXML does do a remarkable job of making it'dare we say'simple to interact with XML. When you want to read a configuration file written in XML, parse an RSS feed, or process the result of a REST request, SimpleXML excels at these tasks. It doesn't work well for more complex XML-related jobs, such as reading a document where you don't know the format ahead of time or when you need to access processing instructions or comments.

SimpleXML turns elements into object properties. The text between the tags is assigned to the property. If more than one element with the same name lives in the same place (such as multiple <people>s), then they're placed inside a list.

Element attributes become array elements, where the array key is the attribute name and the key's value is the attribute's value.

To access a single value, reference it directly using object method notation. Let's use this XML fragment as example:

<firstname>David</firstname>

If you have this in a SimpleXML object, $firstname, here's all you need to do to access David:

$firstname

SimpleXML assumes that when you have a node that contains only text, you're interested in the text. Therefore, print $firstname does what you expect it to: it prints David.

Iteration methods, like foreach, are the best choice for cycling through multiple elements. Code for this is shown in later examples.

Attributes are stored as array elements. For example, this prints out the id attribute for the first person element:

<?php $ab = simplexml_load_file('address-book.xml'); // the id attribute of the first person print $ab->person['id'] . "\n"; ?>

which gives you:

1

Example 12-2 contains a more complete example based on this simple address book in XML. It's used in the code examples that follow.

Simple address book in XML

<?xml version="1.0"?> <address-book>     <person >         <!--David Sklar-->         <firstname>David</firstname>         <lastname>Sklar</lastname>         <city>New York</city>         <state>NY</state>         <email>sklar@php.net</email>     </person>     <person >         <!--Adam Trachtenberg-->         <firstname>Adam</firstname>         <lastname>Trachtenberg</lastname>         <city>San Francisco</city>         <state>CA</state>         <email>amt@php.net</email>     </person> </address-book>

Example 12-3 shows how you use SimpleXML to pull out all the first and last names.

Using SimpleXML to extract data

$sx = simplexml_load_file('address-book.xml'); foreach ($sx->person as $person) {     $firstname_text_value = $person->firstname;     $lastname_text_value = $person->lastname;     print "$firstname_text_value $lastname_text_value\n"; } David Sklar Adam Trachtenberg

When you use SimpleXML, you can directly iterate over elements using foreach. Here, the iteration occurs over $sx->person, which holds all the person nodes.

You can also directly print SimpleXML objects, as shown in Example 12-4.

Printing SimpleXML objects

<?php foreach ($sx->person as $person) {     print "$person->firstname $person->lastname\n"; } ?> David Sklar Adam Trachtenberg

PHP interpolates SimpleXML objects inside of quoted strings and retrieves the text stored in them.

12.3.4. See Also

Recipe 12.4 for parsing complex XML documents; Recipe 12.5 for parsing large XML documents; documentation on SimpleXML at http://www.php.net/simplexml; more information about the underlying libxml2 C library at http://xmlsoft.org/.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net