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

Adding multiple nodes to an XML document - 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.

  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



In the previous section, you learned how to build an XML document from scratch and how to add a single root node to it. Nevertheless, this is merely a basic example of what you can do with the DOM XML extension. Now let me go one step forward and show you another practical example aimed at demonstrating how to insert multiple nodes into an existing XML document.

The corresponding code sample is as follows:

$dom=new DOMDocument('1.0','iso-8859-1');


$secondElement=$dom->createElement('secondnode','This is the second element of the new DOM document');

$thirdElement=$dom->createElement('thirdnode','This is the third element of the new DOM document');

// insert the new elements into the document




// 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"?>


<secondnode>This is the second element of the new DOM document</secondnode>

<thirdnode>This is the third element of the new DOM document</thirdnode>



As shown previously, adding several nodes to an existing XML document requires using exactly the same methods that you learned in the first example. In this  case, the XML document is built through the corresponding “DOMDocument” constructor and the successive elements are created and appended in that order by using the “createElement()/appendChild()” combination.

Finally, all the XML data is echoed naturally to the browser via the “saveXML()” method by sending the appropriate “Content-Type” HTTP header to the client. That was really simple to do. Don’t you think so?

At this moment, and with these two examples still spinning in your mind, you may have noticed the remarkable similarity between the DOM XML API and the one used when working with JavaScript, which can facilitate learning how to perform basic tasks on XML documents with this PHP extension.

Indeed I don’t want to get away from the main goal of this article. Therefore, now that you understand how to build a basic XML document as well as how to insert a few simple nodes into it, it’s time to move forward and learn how to create XML data from an existing array.

This is an interesting topic that will be discussed in detail in the next section. Thus jump ahead and keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: