PHP
  Home arrow PHP arrow Page 2 - A Quick Overview of the XML DOM Extension in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

A Quick Overview of the XML DOM Extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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 ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek