Team LiB
Previous Section Next Section

Accessing XML Data Using SimpleXML

SimpleXML is an extension that provides a simplified, very convenient interface for extracting XML data with minimum fuss or overhead and associating it with ordinary PHP objects that can then be manipulated or accessed using standard PHP tools and techniqueswithout the need for an additional set of functions or properties provided by an extension.

Because SimpleXML is compiled into PHP5 distributions by default, unless your operating system vendor has disabled it, SimpleXML should already be easily available to you without any additional work on your part if you are using PHP5.

SimpleXML is not available to users of PHP4.

Using SimpleXML in PHP Scripts

To use SimpleXML, you need only create a SimpleXMLElement object variable containing XML data. After XML data has been loaded, any value in the object tree can be accessed the same as you would access arrays or other variable data in PHP, as you'll see in the next section.

To create a SimpleXMLElement object and load XML data into it, you can use either of two functions, depending on your needs and the way in which your XML data is stored and accessed:

  • Use simplexml_load_file() if your XML data needs to be loaded and parsed from a text file. Pass the pathname of the file as an argument; the SimpleXMLElement object will be returned if SimpleXML is able to load and parse the file.

  • Use simplexml_load_string() if you have a PHP string variable or constant that you want to parse into a SimpleXMLElement object; the resulting SimpleXMLElement object will be returned if SimpleXML is able to parse the string data.

  • Use simplexml_import_dom() if you have a DomDocument object containing an XML object tree that you want to parse into a SimpleXMLElement object; the resulting SimpleXMLElement object will be returned if SimpleXML is able to import the DomDocument object.

To access data or elements in the XML object tree after it has been loaded and parsed into a SimpleXMLElement object, use these techniques:

  • To access element values, call nodes of the XML tree by name as if they were properties of the SimpleXMLElement object. The value of the element will be returned as an element object.

  • In cases where more than one element of a given name exists at the same level, include in brackets the number of the element that you want to access, as though the element in question was an array.

  • To access element attributes, call nodes of the XML tree by name as necessary, taking care to include the name of the attribute in brackets, as though the final element in the list was an array. The value of the element attribute in question will be returned as an element object.

Some examples of these techniques on a SimpleXMLElement object called $my_xml are shown in Table 9.10.

Table 9.10. Accessing Element Values Using SimpleXML

Sample

Meaning

$my_xml->title

Returns an element object containing the value of the top-level <title> element.

$my_xml->car->engine->displacement

Returns an element object containing the value of the <displacement> element that appears within the <engine> element that appears within the <car> element.

$my_xml->book[4]->author

Returns an element object containing the value of the <author> element that appears within the fourth <book> element.

$my_xml->book[4]->author['gender']

Returns an element object containing the attribute value for the gender attribute of the <author> element that appears within the fourth <book> element.


As you can see, SimpleXML provides for perhaps the cleanest, easiest-to-use interface for accessing XML data within PHP scripts.

Additional Notes About SimpleXML in PHP Scripts

In spite of the apparent simplicity presented by the SimpleXML interface, a few additional details should be supplied to prospective PHP5 with SimpleXML script writers:

  • Because SimpleXML returns an element object each time you access an element or attribute value, you must remember in many cases to cast the object to a string before making comparisons against string objects or string literals. This can be done simply by prefixing the object access with (string), as shown in this example:

    if ((string)$my_xml->book[4]->title == 'War and Peace') {
        echo 'Book four is by Tolstoy, we must pause here!';
        exit;
    }
    

  • SimpleXML objects can be manipulated in many of the same ways that other PHP objects can, including techniques such as iteration. For example, it wouldn't be uncommon to see a SimpleXML object included in a loop this way:

    foreach ($my_xml->book as $book) {
        echo 'Book is called ', $book->title;
        echo 'Book is by ', $book->author;
    }
    

  • When using SimpleXML, you can also use XPath patterns of the type discussed earlier in reference to XSLT templates (see Table 9.2). To do so, use the xpath() property with the pattern whose value you want to access:

    $my_address = (string)$my_xml->xpath(//smallsville//phone_book/john_smith);
    

  • As you might expect, you can also set element and attribute values when using SimpleXML, provided you have already loaded an XML tree into a SimpleXMLElement object. Simply assign values as you would to any other variable:

    $my_xml->personal_data->phone_number = '123-456-7890';
    

    Team LiB
    Previous Section Next Section