PHP
  Home arrow PHP arrow Page 4 - 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 - Adding nodes to a simple XML document by using array elements
    ( Page 4 of 4 )

    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
     

       

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek