PHP
  Home arrow PHP arrow Page 4 - 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 Developerworks
 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 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 4 hosted by Hostway