SimpleXML is a new addition to PHP 5; it is enabled by default and requires no additional installation or configuration steps. It lives up to its description in the PHP Manual of being "a very simple and easily usable toolset to convert XML" while still being powerful. Unlike the DOM family of functions, there are only a few SimpleXML functions and methodsseven, at current count. The most basic SimpleXML function parses the XML data into an object that can be directly accessed and manipulated without special functions to do so. The first function you need to know about is simplexml_load_file(), which loads a file and creates an object out of the data: $object_with_data = simplexml_load_file("somefile.xml"); In Listing 28.2, a short bit of code is used to create a SimpleXML object and then display the hierarchy of the data stored in the object. Listing 28.2. Load and Display Data Using SimpleXML
In line 2, the contents of books.xml are loaded using simple_load_file() into an object called $theData. In line 4, the print_r() function is used to output a human-readable version of the data stored in the object, surrounded by the <pre></pre> tag pair. Save this listing as simplexml_dump.php and place it in the document root of your web server. When viewed through your web browser you should see something like Figure 28.2. Figure 28.2. Data dumped from a SimpleXML object.Dumping out data isn't all that spectacular, but it does show you the structure of the object, which in turn lets you know how to access the data in a hierarchical fashion. For instance, the output of simplexml_dump.php shows the entry for a book: [0] => SimpleXMLElement Object ( [Title] => A Very Good Book [Author] => Jane Doe [PublishingInfo] => SimpleXMLElement Object ( [PublisherName] => Sams Publishing [PublisherCity] => Indianapolis [PublishedYear] => 2006 ) ) To reference this record directly, you would use $theData->Book The elements in the record would be accessed like this:
But because you likely would want to loop through all the records and not just the first one, the references to the data are a little different, as you can see in Listing 28.3. Listing 28.3. Through an XML Document Using SimpleXML
In line 2, the contents of books.xml are loaded using simple_load_file() into an object called $theData. In line 4, the contents of $theData->Book, which is to say all the individual records, are put into an array called $theBook. Lines 59 gather the value of specific elements, beginning at the level of $theBook, and these values are output in lines 1115. Lines 1720 unset the value of the variables for the next pass through the loop. Save this listing as simplexmlexample.php and place it in the document root of your web server. When viewed through your web browser you should see something like Figure 28.3. Figure 28.3. Text extracted and displayed using SimpleXML functions.For more information on the SimpleXML functions in PHP, visit the PHP Manual at http://www.php.net/simplexml. |