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!
PHP has always been ahead of the curve when it comes to supporting new technologies; XML is no exception. Early versions of PHP came with basic XML support built in; newer versions improved on this by adding support for new XML protocols and technologies like the DOM, WDDX and SOAP, making PHP one of the most versatile and flexible tools for XML application development.
Now, by default, all newer versions of PHP come with the XML SAX parser enabled; however, the DOM module needs to be explicitly turned on at compile time. If you don't have the ability to recompile your PHP build -- and if you're sharing space on a server, it's quite likely you won't –- then you're up a creek without a paddle if your application needs to dynamically create XML document instances.
So, a creative solution is needed -- which is where this article comes in.
Over the next few pages, I'm going to be introducing you to a free PHP class named XML_Serializer, which allows you to create XML documents from PHP data structures like arrays and objects without requiring you to first recompile your PHP build for DOM support. As you might imagine, this can come in handy in certain situations - for example, if you need a quick and dirty way to build an XML document tree from an external data source, like a MySQL database or a structured text file. So keep reading – you might find the rest of this show interesting!