Home arrow PHP arrow Page 4 - A Quick Overview of the XML DOM Extension in PHP 5

Adding nodes to a simple XML document by using array elements - PHP

To be frank, how many times in your life as a PHP developer have you had to deal with XML documents? Probably dozens of times, particularly if you use it to develop PHP applications that implement some kind of web service, or possibly when creating RSS feeds and template files for different web sites. And, surely, the list goes on and on.

TABLE OF CONTENTS:
  1. A Quick Overview of the XML DOM Extension in PHP 5
  2. Building XML documents from scratch
  3. Adding multiple nodes to an XML document
  4. Adding nodes to a simple XML document by using array elements
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
February 26, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: