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!
One of XML_Serializer's other interesting features is its ability to store data type information along with each value in the XML document. Called typeHints, this data type information can help in distinguishing between the integer 6 and the string "6", and comes in handy if your XML application is strongly typed.
To enable type hints, you need to simply set the typeHints option to true. The following example illustrates:
// check result code and display XML if success if($result === true) { echo $serializer->getSerializedData(); }
? >
Once type hints are enabled, every element within the XML document will bear an additional attribute indicating the data type of the value contained within it. Here's what the output of the example above looks like:
Note that in the example above, I've used a slightly different method to set serializer options -- I've created an array of options and values, and passed the array to the object constructor. When you have a large number of options to set, this method can save you a few lines of code.