PHP
  Home arrow PHP arrow Page 2 - 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 
 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    A Quick Overview of the XML DOM Extension in PHP 5 - Building XML documents from scratch
    (Page 2 of 4 )

    The first thing that I’m going to show you concerning the use of the DOM XML extension with PHP 5 is pretty basic. It consists of building an XML document from scratch, adding a root element to it, and finally outputting this raw data on the browser.

    The following script performs all of the tasks described above, so analyze its definition in detail:


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

    $element=$dom->createElement('testnode','This is the root element of the new DOM document');

    // insert the new element as root (child of the document)

    $dom->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"?>

    <testnode>This is the root element of the new DOM document</testnode>

    */


    Now that you have taken a close look at the previous script, it’s time to dissect it into separate parts and see what it does. First a new, empty DOM document is created through an instance of the “DOMDocument” class, which as you’ll see in subsequent examples, will always be the starting point for building XML documents.

    Then a root element is created with the pertinent “createElement()” method and inserted into the document via another one, called “appendChild(),” which comes in handy when appending new elements to an existing document tree. In the end, these recently added contents are printed to the browser by using the “saveXML()” method. This method's function is simply to return the XML data just created to client code. Quite simple to understand, right?

    At this point, you've learned how to build a simple XML document, which only contains a root element, from scratch. It’s clear to see in the previous example how the DOM API is utilized. For instance, it can be used to append new nodes to a given XML document via the "createElement()/appendChild()” methods used in tandem. As you saw in the prior example, the first method takes up two primary arguments: one is the name of the node to be created, while the other is its respective text value (or in other words, its text node).

    Having demonstrated how to construct a basic XML document with the DOM XML PHP extension, I’m pretty sure that you’ll find numerous similarities when working with the DOM and JavaScript, since you can also create new elements, copy and remove new nodes from the web document tree, and so forth.

    However, I’m only scratching the surface when it comes to exploring the methods provided by the DOM XML extension, which means that there is still a long way ahead of us. Therefore, in the upcoming section, I’m going to show you how to add multiple nodes to an XML document.

    Want to see how this will be done? Click on the link below 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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