CHAPTER 8 – XML with PHP 5 – Creating a DOM Tree

The DOM extension can do more than parse XML. It can create an XML document from scratch. In your script, you can build a tree of objects that you can dump to disk as an XML file. This ideal way to write XML files is not easy to do from within a script, but we're going to do it anyway. In this example, we create a file with content similar to that shown in the example XML file we used in the previous section. We cannot guarantee that the file will be exactly the same because the DOM extension might not handle the whitespace in the XML file as cleanly as a human would. Let's start by creating the DOM object and the root node:

<?php $dom = new DomDocument();

$html = $dom->createElement('html'); $html->setAttribute("xmlns", "http://www.w3.org/1999/xhtml"); $html->setAttribute("xml:lang", "en"); $html->setAttribute("lang", "en"); $dom->appendChild($html); First, a DomDocument class is created with new DomDocument(). All elements are created by calling the createElement() method of the DomDocument class or createTextNode() for text nodes. The name of the element--in this case, html-- is passed to the method, and an object of the type DomElement is returned. The returned object is used to add attributes to the element. After the DomElement has been created, we add it to the DomDocument by calling the appendChild() method. Then, we add the head to the html element and a title element to the head element: $head = $dom->createElement('head'); $html->appendChild($head); $title = $dom->createElement('title'); $title->appendChild($dom->createTextNode("XML Example")); $head->appendChild($title); As before, we first create a DomElement object (for example, head) by call- ing the createElement() method of the DomDocument object, and then we add the newly created object to the existing DomElement object (for example, $html) with appendChild(). We then add the body element with its background attribute. Then, we add the 'p' element, which contains the main content of our X(HT)ML document, as a child of the body element: /* Create the body element */ $body = $dom->createElement('body'); $body->setAttribute("backgound", "bg.png"); $html->appendChild($body); /* Create the p element */ $p = $dom->createElement('p'); $body->appendChild($p); The contents of our <p> element are more complicated. It consists (in order) of a text element ("Moved to "), an <a> element, another text element (our dot), the <br> element, and finally, a third text element ("foo & bar"): /* Add the "Moved to" */ $text = $dom->createTextNode("Moved to "); $p->appendChild($text); /* Add the a */ $a = $dom->createelement('a'); $a->setAttribute("href", "http://example.org/"); $a->appendChild($dom->createTextNode("example.org")); $p->append_child($a); /* Add the ".", br and "foo & bar" */ $text = $dom->createTextNode("."); $p->appendChild($text); $br = $dom->createElement('br'); $p->appendChild($br); $text = $dom->createTextNode("foo & bar"); $p->appendChild($text); When we're finished creating the DOM of our X(HT)ML document, we echo it to the screen: echo $dom->saveXML(); ?> The output resembles our original document, but without some of the whitespace (which is added here for readability): <?xml version="1.0"?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>XML Example</title> </head> <body background="bg.png"> <p>Moved to <a href="http://example.org/">example.org</a>. <br>foo &amp; bar</p> </body> </html>

Post Comment
Login to post comments