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!
Now, if you're a nitpicker, the output of the example on the previous page still won't satisfy you. Here's why:
The serialized XML document does not contain the XML declaration at the top.
The root element of the document is called <array>, whereas what you actually want is for it to be <library>.
The XML document is not correctly indented.
In order to account for these requirements, XML_Serializer comes with a setOption() method, which allows you to customize the behaviour of the serializer to your needs. To illustrate, consider the following example, which solves the first problem noted above:
<?php
// include class file include("Serializer.php");
// create object $serializer = new XML_Serializer();
Thus, the setOption() method takes two arguments -- a variable and its value -- and uses that information to tell the serializer how to return the XML document.
Next, how about fixing the root element and the indentation?
<?php
// include class file include("Serializer.php");
// create object $serializer = new XML_Serializer();