Article

PHP5: Coming Soon to a Webserver Near You

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Next

Simple XML

The Simple XML extension introduces a new type of parser to PHP, using what is generally known as an "Object Mapping XML API" ( O'Reilly recently ran A Survey of APIs and Techniques for Processing XML—see it for more detail ).

If you're familiar with the DOM API, you'll know that it creates from an XML document a tree structure containing objects that represent the XML elements it found. As the above DOM HTML parsing example demonstrates, there's still a lot of work for a developer to convert these into something they can use within an application.

An "Object Mapping XML API" takes a different view, which can help make life far easier for developers. Like the DOM API, it builds a tree structure upon reading an XML document but, unlike the DOM API, it converts the XML elements into the native data types used by the programming language in question. This means that the moment the document is parsed, you can begin to use the results directly.

The SimpleXML extension to PHP provides a mechanism to quickly convert simple XML documents into native PHP types. For example, if I have an XML document like this:

               
<?xml version="1.0"?>                
<config>                
 <email>sysadmin@sitepoint.com</email>                
 <database>                
   <host>localhost</host>                
   <type>MySQL</type>                
   <user>harryf</user>                
   <pass>secret</pass>                
   <dbname>sitepoint</dbname>                
 </database>                
</config>                

Parsing it now looks like:

               
<?php                
$xml = file_get_contents('config.xml');                
               
$config = simplexml_load_string($xml);                
               
echo ('<pre>');                
print_r($config);                
echo ('</pre>');                
?>                

Script: simplexml.php

The output is:

               
simplexml_element Object                
(                
   [email] => sysadmin@sitepoint.com                
   [database] => simplexml_element Object                
       (                
           [host] => localhost                
           [type] => MySQL                
           [user] => harryf                
           [pass] => secret                
           [dbname] => sitepoint                
       )                
               
)                

In practice, this might mean the following code is possible:

               
<?php                
// Get the file                
$xml = file_get_contents('config.xml');                
               
// Convert the file to native PHP variables                
$config = simplexml_load_string($xml);                
               
// Copy the database node to a new variable                
$db = $config->database;                
               
try {                
   // Connect to MySQL using the database information from the XML doc                
   if ( !@mysql_connect($db->host,$db->user,$db->pass) )                
       throw new Exception('Database Connect Error'.mysql_error());                
               
} catch (Exception $e) {                
               
   // Send an email using the email variable                
   mail ( $config->email,$e->getMessage() );                
}                
?>                

Script: simplexml1.php

Notice how, after parsing the XML document with SimpleXML, I can immediately begin using the data?

The SimpleXML extension does have limitations. In particular, I found it ignores XML attributes and can only deal with XML documents of up to three levels of node in depth, including the root node. At first glance that may seem like a serious limitation, but when you consider the DOM extensions dump_node() method, SimpleXML could be a very useful partner when using DOM to parse an XML document, eliminating a whole bunch of (irritating) calls you have to make to get to the data you want. Note, however, that with the Beta, there seems to be a bug which prevents SimpleXML and DOMXML being used in the same script.

Otherwise, given libxml's support for XML schema, the approach taken by SimpleXML could be expanded upon to convert complete XML documents straight into native PHP types; very nice indeed, thank you!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links