Simplify the task of creating XML documents with the XML_Serializer class, which lets you build nested XML documents from PHP data structures like arrays and objects (and vice versa). I'll show you how to build an XML document tree via the XML_Serializer class from PEAR, how to programmatically create an XML document from an array or an object, how to attach attributes to elements, and how to customize the behavior of the serializer. All this, and much, much more!
It's also possible to convert an XML document into a PHP object instead of a nested set of arrays, simply by setting appropriate options for the unserializer. Consider the following example, which demonstrates how this may be done:
<?php
// include class file include("Unserializer.php");
// tell the unserializer to create an object $options = array("complexType" => "object");
// unserialize the document $result = $unserializer->unserialize("library.xml", true);
// dump the result print_r($unserializer->getUnserializedData());
? >
Here's the output:
stdClass Object
( [book] => Array ( [0] => stdClass Object ( [title] => The Adventures of Sherlock Holmes [author] => Arthur Conan Doyle [price] => 24.95 )
[1] => stdClass Object ( [title] => Life of Pi [author] => Yann Martel [price] => 7.99 )
[2] => stdClass Object ( [title] => Europe on a Shoestring [author] => Lonely Planet [price] => 16.99 ) ) )
In this format, you can use standard object notation to access (for example) the title of the last book. The notation
$obj
->book[2]->title
would return
Europe on a Shoestring
* Employment Options
Now, while all this is fine and dandy, how about using all this new-found knowledge for something practical?
This next example does just that, demonstrating how the XML_Serializer class can be used to convert data stored in a MySQL database into an XML document, and write it to a file for later use. Here's the MySQL table I'll be using,
mysql
> SELECT * FROM employees; +-----+--------+--------+-----+-----+----------------+---------+ | id | lname | fname | age | sex | department | country | +-----+--------+--------+-----+-----+----------------+---------+ | 54 | Doe | John | 27 | M | Engineering | US | | 127 | Jones | Sue | 31 | F | Finance | UK | | 113 | Woo | David | 26 | M | Administration | CN | | 175 | Thomas | James | 34 | M | Finance | US | | 168 | Kent | Jane | 29 | F | Administration | US | | 12 | Kamath | Ravina | 35 | F | Finance | IN | +-----+--------+--------+-----+-----+----------------+---------+ 6 rows in set (0.11 sec)
and here's what I want my target XML document to look like:
// open file if (!$handle = fopen($filename, 'w')) { print "Cannot open file ($filename)"; exit; }
// write XML to file if (!fwrite($handle, $serializer->getSerializedData())) { print "Cannot write to file ($filename)"; exit; }
// close file fclose($handle);
? >
Pretty simple, once you know how it works. First, I've opened up a connection to the database and retrieved all the records from the table. Then I've instantiated a new document tree and iterated over the result set, adding a new set of nodes to the tree at each iteration. Finally, once all the rows have been processed, the dynamically generated tree is written to a file for later use.