Dynamic XML document construction with the PHP DOM

This tutorial is sourced directly from techrepublic.com, but it fits in perfectly with my tutorial on dynamic JavaScript creation so I’ve posted it here too; I found it fantastic, and figured it was best shared with everyone. Original post here. Don’t just follow through to their link, because I have included it here as a perfect complement to some of my other tutorials.

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. Over the course of this article, I’ll be introducing you to 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.

Note: This article assumes a working Apache/PHP5 installation with the DOM functions enabled, and a working knowledge of basic XML constructs such as elements, attributes and CDATA blocks. You can obtain an introduction to these topics from the introductory material at Melonfire.

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.

Listing A

<?php
// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");

// save and display tree
echo $dom->saveXML();
?>

Notice the saveXML() method of the DOMDocument object — I’ll come back to this later, but for the moment simply realize 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:

<?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 DOMDocumentinitialized, 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’screateElement() 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.

An example will make this clearer. Consider the script in Listing B.

Listing B

<?php
// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");

// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);

// create child element
$item = $dom->createElement("item");
$root->appendChild($item);

// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);

// save and display tree
echo $dom->saveXML();
?>

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:

<?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).

Listing C

<?php
// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");

// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);

// create child element
$item = $dom->createElement("item");
$root->appendChild($item);

// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);

// create child element
$item = $dom->createElement("item");
$root->appendChild($item);

// create another text node
$text = $dom->createTextNode("tomato");
$item->appendChild($text);

// save and display tree
echo $dom->saveXML();
?>

And here’s the revised output:

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

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 DOMDocumentobject’s createAttribute() method, and then append a text node to it holding the attribute value. Listing D is an example.

Listing D

<?php
// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");

// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);

// create child element
$item = $dom->createElement("item");
$root->appendChild($item);

// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);

// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);

// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);

// save and display tree
echo $dom->saveXML();
?>

And here’s what the output will look like:

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

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’screateCDATASection() and createProcessingInstruction() methods. Listing E shows you an example.

Listing E

<?php
// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");

// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);

// create child element
$item = $dom->createElement("item");
$root->appendChild($item);

// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);

// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);

// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);

// create CDATA section
$cdata = $dom->createCDATASection("\nCustomer requests that pizza be sliced into 16 square pieces\n");
$root->appendChild($cdata);

// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);

// save and display tree
echo $dom->saveXML();
?>

And here’s the output:

<?xml version="1.0"?>
<toppings>
<item price="4">pepperoni</item>
<![CDATA[
Customer requests that pizza be sliced into 16 square pieces
]]>
<?pizza bake()?>
</toppings>

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).

Listing F

<?php
// create doctype
$dom = new DOMDocument("1.0");

// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
$dom->formatOutput=true;

// create child element
$item = $dom->createElement("item");
$root->appendChild($item);

// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);

// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);

// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);

// create CDATA section
$cdata = $dom->createCDATASection("\nCustomer requests that pizza be sliced into 16 square pieces\n");
$root->appendChild($cdata);

// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);

// save tree to file
$dom->save("order.xml");

// save tree to string
$order = $dom->save("order.xml");
?>

And that’s about it. Hopefully you found this article interesting, and will be able to use these techniques in your daily work with XML. Happy coding!

Advertisements

php for the soul, plus other stuff

Well, here we go. I came up with a “new” website concept on Saturday night so, you guessed it, my assignment got forgotten and I did an all-nighter. Long story short, it’s pretty much all finished, just doing touch-ups to it now. I’ll leave descriptions etc til it’s online, which is hopefully in next few days. Main issue is finding an available domain name!! I have finally gained an appreciation of the hatred felt for domain squatters. That, and how frustrating it is to attempt to name a blog….

Back to the other day, when I decided I better get it together and become another WordPress blogger, it was unbelievable how many 14 yr-old girls had decided to start blogs in 2004/2005 and made the first post then never again!! Seriously! And, before you say I was choosing dumb names (ie that 14 yr-old girls would choose), I will refer you to possible options:

  • myblog – 12 yr-old girl, couldn’t even post, only made comments to the default posts and pages (Sept 23, 2005)
  • qwerty – I take the “14 yr-old girls” bit back, nothing doing here (Nov 2, 2005)
  • ifelse – no idea who, China (end of 2005)
  • rofl – one hit wonder (Oct 23, 2005)
  • over9000 – Eastern European, the most used one so far (31 Aug – 27 Sept 2007)

I’ll stop there before I have a heart attack (check this out if you want one too!!). Anyway, moving on, I found a name (and not a bad one, may I say) and here it is.

In other news….my  RSS feed for this blog is up and rolling (Wahoo!), and I have got a whole lot of stuff done with php. So, here’s a lovely tid-bit as promised:

I’ve just been using
$ip=$_SERVER['REMOTE_ADDR'];
to get the ip of my site visitor. This is correct, but a bit of geeking the ‘net today led to this bit of work by scriptgoddess, and then built out a bit:
function getAddress() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}

As a quick run down, if it doesn’t come absolutely clearly to you, we first check for $_SERVER['HTTP_CLIENT_IP'] (shared internet), and allocate that that as the address. If this isn’t available, we then move to $_SERVER['HTTP_X_FORWARDED_FOR'] (the ip a proxy was forwarding on behalf of), if required; otherwise we default to $_SERVER['REMOTE_ADDR']. This provides us with the most precise “real” ip address for the user visiting our site. The main hurdle we have overcome now is the inability to accurately map geo-demographics visiting your site. Purely using $ip=$_SERVER['REMOTE_ADDR'] will result in some ISPs and proxies claiming stupid volumes of traffic, while not allowing you to accurately target your content (and advertising!!).

I have to say I’m getting over the layout of my code in this theme (I just changed it twice), so I’m going to give up on posting code for now, and finish my web work. One of tomorrow’s tasks is now to find a new theme I like that supports displaying code in a useful manner.