PHP
  Home arrow PHP arrow Page 2 - Working with Attributes and CDATA Sect...
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

Working with Attributes and CDATA Sections with the DOM XML Extension
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-04

    Table of Contents:
  • Working with Attributes and CDATA Sections with the DOM XML Extension
  • Reviewing some methods of the DOM XML extension
  • Working with the createAttribute() method
  • Inserting CDATA nodes into an XML document

  • 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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    Working with Attributes and CDATA Sections with the DOM XML Extension - Reviewing some methods of the DOM XML extension
    (Page 2 of 4 )

    Before I begin showing you how to use the DOM XML extension that comes packaged with PHP, first I will rehash the methods that were covered in the previous installment of the series, in case you weren’t able to read it.

    In that specific tutorial, I introduced a few basic methods, such as “createElement()”and “appendChild(),” which were pretty useful for building and inserting new nodes into an existing XML document. I culminated the article by developing a complete practical example where all these methods, along with an additional one called “saveXML(),” were utilized to build a new XML document populated by using a basic PHP array. The source code corresponding to this example is listed below. Here it is:


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

    */


    As you can see, the above example speaks for itself, due to its extreme simplicity. In this case, a simple PHP array is used to create and append a few new nodes to an XML document via the pair of “createElement()” and “appendChild()” methods I mentioned above. Once those nodes are appended to the document tree, the whole structure is printed on screen through the “saveXML()” method. That wasn’t too difficult to grasp, was it?

    With the previous example still fresh in your mind, I assume that using the DOM XML PHP extension to build a basic XML document from scratch and add new nodes to it is already a familiar process for you. This means that it's time to continue learning more useful methods that come integrated with this extension.

    In the following section, I’m going to teach you how to add some custom attributes to the existing nodes of a recently-created XML document and how to insert CDATA sections into it. All of these topics will be treated in depth in the next few lines, so click on the link that appears below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · The DOM XML PHP library comes equipped with a bunch of methods that can be useful...
     

       

    PHP ARTICLES

    - 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
    - Working with Multiple Document Nodes with th...




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