Dynamic XML in PHP

Creating dynamic XML with PHP

When working with XML-based applications, developers often find themselves facing the requirement to generate XML-encoded data structures on the fly. Examples of this include an XML order template based on user input in a Web form, or an XML representation of a server request or client response based on run-time parameters.

Although this task might seem intimidating, it's actually quite simple when one takes into account PHP's sophisticated DOM API for dynamic node construction and manipulation. In this article, you will get know the main functions in this API, showing you how to programmatically generate a complete well-formed XML document from scratch and save it to disk.

Creating the Doctype declaration

Let's start right at the top, with the XML declaration. In PHP, this is fairly simple; it only requires you to instantiate an object of the DOMDocument class and supply it with a version number. To see it in action type out the example script in Listing A.

Notice the saveXML() method of the DOMDocument object -- I'll come back to this later, but for the moment simply realise that this is the method used to output a current snapshot of the XML tree, either to a file or to the browser. In this case, I've sent the output directly to the browser as ASCII text for readability purposes; in real-world applications, you would probably send this with a Content-Type: text/xml header.

When you view the output in your browser, you should see something like this:

Code: XML

<?xml version="1.0"?>

Adding elements and text nodes

Now that's all very pretty and fine, but the real power of XML comes from its elements and the content they enclose. Fortunately, once you've got the basic DOMDocument initialised, this becomes extremely simple. There are two steps to the process:

1. For each element or text node you wish to add, call the DOMDocument object's createElement() or createTextNode() method with the element name or text content. This will result in the creation of a new object corresponding to the element or text node.

2. Append the element or text node to a parent node in the XML tree by calling that node's appendChild() method and passing it the object produced in the previous step.

Here, I've first created a root element named <toppings> and attached it to the XML header. Next, I've created an element named <item> and attached it to the root element. And finally, I've created a text node with the value "pepperoni" and attached it to the <item> element. The result should look like this:

Code: XML

<?xml version="1.0"?><toppings><item>pepperoni</item></toppings>

If you'd like to add another topping, simply create another <item> and populate it with different content (Listing C).

Adding attributes

You can also add qualifying information to your elements, through the thoughtful use of attributes. With the PHP DOM API, attributes are added in a two-step process: first create an attribute node holding the name of the attribute with the DOMDocument object's createAttribute() method, and then append a text node to it holding the attribute value. Listing D is an example.

Adding CDATA blocks and processing instructions

While not used quite as often, CDATA blocks and processing instructions (PI) are also well-supported by the PHP API, through the DOMDocument object's createCDATASection() and createProcessingInstruction() methods. Listing E shows you an example.

Saving the results

Once you've got the tree the way you want it, you can either save it to a file or store it in a PHP variable. The former function is performed by calling the save() method with a file name, while the latter is performed by calling the saveXML() method and assigning the result to a string. Here's an example (Listing F).