PHP
  Home arrow PHP arrow Page 3 - A Quick Overview of the XML DOM Extens...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

A Quick Overview of the XML DOM Extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2008-02-26

    Table of Contents:
  • A Quick Overview of the XML DOM Extension in PHP 5
  • Building XML documents from scratch
  • Adding multiple nodes to an XML document
  • Adding nodes to a simple XML document by using array elements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    A Quick Overview of the XML DOM Extension in PHP 5 - Adding multiple nodes to an XML document
    (Page 3 of 4 )

    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');

    $rootElement=$dom->createElement('rootnode','');

    $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

    $dom->appendChild($rootElement);

    $rootElement->appendChild($secondElement);

    $rootElement->appendChild($thirdElement);

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

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

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

    </rootnode>

    */


    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


       · PHP 5 comes equipped with a useful extension, called DOM XML, which its name...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway