PHP
  Home arrow PHP arrow Page 3 - 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 
eWeek
 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
     
     
    CIO Insight
     
    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 - Working with the createAttribute() method
    (Page 3 of 4 )

    Undoubtedly, one of the most common operations involved in the manipulation of XML documents is the creation of custom attributes that can be appended to one or more nodes. To perform this specific task, the DOM XML extension provides an intuitive method called “createAttribute(),” which can be used to dynamically build new attributes.

    Before I show you a concrete example of how to use this specific method, you should be aware that once a new attribute has been created, it must be inserted into the pertinent XML document via the “appendChild()” method (or a similar one). As you’ll see in a moment, all of the attributes should be appended to the document tree, as it is done with regular nodes.

    Having clarified this point, pay attention to the following example, which shows how to use the method in question to assign a few simple attributes to several nodes of an existing XML document.


    // example on creating a new DOMDocument object and appending an attribute using the 'createAttribute()' method

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

    // create attribute

    $attribute=$dom->createAttribute('customattribute');

    // append attribute to element

    $element->appendChild($attribute);

    // append element to document

    $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 customattribute="">Element 1</element1>

    <element2 customattribute="">Element 2</element2>

    <element3 customattribute="">Element 3</element3>

    <element4 customattribute="">Element 4</element4>

    <element5 customattribute="">Element 5</element5>

    <element6 customattribute="">Element 6</element6>

    <element7 customattribute="">Element 7</element7>

    <element8 customattribute="">Element 8</element8>

    <element9 customattribute="">Element 9</element9>

    <element10 customattribute="">Element 10</element10>

    </rootnode>

    */


    As shown by the previous example, adding a custom attribute to different nodes of a given XML document is actually a no-brainer process that can be performed with minor effort. In this case, I used the “createAttribute()” method to construct the attribute in memory, and then insert it into the document tree via the pertinent “appendChild()” method.

    Lastly, the recently-created document is outputted to the browser through the “saveXML()” method that I introduced in the preceding article of this series. It’s worthwhile to mention two things here: first, no specific values have been assigned to the respective attributes, and second, all of the child nodes in the above XML document are dynamically generated from a basic PHP array.

    Of course, the structure of the previous XML document is rather incomplete, since its attributes don’t contain a specific value. However, don’t worry about this issue for the time being, since in upcoming articles of this series, I’ll be discussing how to do this in detail.

    So far, so good, right? At this time, you've hopefully learned how to insert some custom attributes into an existing XML document by using the API of the DOM XML extension. So, where do we go from here? Good question. In the last section of this article, I’ll teach you how to insert CDATA sections into a given XML string.

    However, to see how this will be done, you’ll have to read the next few lines. Just click on the link below.

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

     
    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 6 hosted by Hostway