Accessing XML in PHP Using SimpleXML Functions


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

 1: <?php 2: $theData = simplexml_load_file("books.xml"); 3: echo "<pre>"; 4: print_r($theData); 5: echo "</pre>"; 6: ?>

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:

  • $theData->Book->Title for the Title

  • $theData->Book->Author for the Author

  • $theData->Book->PublishingInfo->PublisherName for the Publisher Name

  • $theData->Book->PublishingInfo->PublisherCity for the Publisher City

  • $theData->Book->PublishingInfo->PublishedYear for the Published year

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

 1:  <?php 2:  $theData = simplexml_load_file("books.xml"); 3: 4:  foreach($theData->Book as $theBook) { 5:      $theBookTitle = $theBook->Title; 6:      $theBookAuthor = $theBook->Author; 7:      $theBookPublisher = $theBook->PublishingInfo->PublisherName; 8:      $theBookPublisherCity = $theBook->PublishingInfo->PublisherCity; 9:      $theBookPublishedYear = $theBook->PublishingInfo->PublishedYear; 10: 11:     echo " 12:     <p><span style=\"text-decoration:underline\">".$theBookTitle."</span> 13:     by ".$theBookAuthor."<br/> 14:     published by ".$theBookPublisher." (".$theBookPublisherCity.") 15:     in ".$theBookPublishedYear."</p>"; 16: 17:     unset($theBookTitle); 18:     unset($theBookAuthor); 19:     unset($theBookPublisher); 20:     unset($theBookPublishedYear); 21: } 22: ?>

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.




Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

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