As you saw in the prior section, inserting a few basic nodes into a recently-created XML document is actually a no-brainer process that can be tackled with minor hassles, even if you’re just getting started using the DOM XML extension. So, assuming that you grasped the logic that stands behind this procedure, I’m going to show you another concrete example where different nodes are added to a new XML document, but in this case using a simple PHP array. Having outlined how this brand new example will work, pay attention to its corresponding source code, which is listed below: $elements=array('element1'=>'Element 1','element2'=>'Element 2','element3'=>'Element 3','element4'=>'Element 4','element5'=>'Element 5','element6'=>'Element 6','element7'=>'Element 7','element8'=>'Element 8','element9'=>'Element 9','element10'=>'Element 10'); $dom=new DOMDocument('1.0','iso-8859-1'); $rootElement=$dom->createElement('rootnode',''); // insert the root element into the document $dom->appendChild($rootElement); // insert additional elements into the document foreach($elements as $key=>$value){ $element=$dom->createElement($key,$value); $rootElement->appendChild($element); } // tell the browser the output is XML via the 'Content-Type' HTTP header header('Content-Type: text/xml'); // display DOM document echo $dom->saveXML(); /* displays the following <?xml version="1.0" encoding="iso-8859-1"?> <rootnode> <element1>Element 1</element1> <element2>Element 2</element2> <element3>Element 3</element3> <element4>Element 4</element4> <element5>Element 5</element5> <element6>Element 6</element6> <element7>Element 7</element7> <element8>Element 8</element8> <element9>Element 9</element9> <element10>Element 10</element10> </rootnode> */ See how simple it is to build a multi-node XML document by utilizing a basic PHP array to construct the nodes in question? I bet you do! In this case, I used the pair of “createElement()/appendChild()” methods to accomplish this basic task, so since you've already seen how they function, you shouldn’t have major problems seeing how the previous example works, right? However, a brief note is in order here: while the logic implemented by the aforementioned example is actually very simple to grasp, it does demonstrate how easy it is to create complete XML documents from an external source, like a PHP array. Obviously, instead of using array elements, you could use a MySQL database table to build an XML-based representation of the stored data utilizing a script similar to the one shown previously. This is only one basic example of what you can do with the DOM XML extension, but you’ll find many others as you climb through the corresponding learning curve. In the meantime, feel free to use all the code samples that were developed in this tutorial to acquire a more solid background when using this powerful PHP library. Final thoughts In this initial article of the series, I provided you with a quick overview of some basic tasks that you can perform with the DOM XML extension that comes bundled with PHP. From creating XML documents from scratch to adding a few simple nodes, all these things are fairly easy to grasp. In the next part of the series, I’m going to show you how to use many other features offered by this useful PHP extension, such as adding attributes to existing nodes of an XML document, creating CDATA sections, and much more. Don’t miss the next article!
blog comments powered by Disqus |
|
|
|
|
|
|
|