PHP
  Home arrow PHP arrow Page 4 - Accessing Attributes and Cloning Nodes...
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 
 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

Accessing Attributes and Cloning Nodes with the DOM XML Extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-04-01

    Table of Contents:
  • Accessing Attributes and Cloning Nodes with the DOM XML Extension in PHP 5
  • Accessing attributes of XML nodes with the getAttribute() method
  • Determining the existence of attributes for a specific XML node with the has Attribute() method
  • Cloning nodes of an XML document with the cloneNode() method

  • 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

    Dell PowerEdge Servers

    Accessing Attributes and Cloning Nodes with the DOM XML Extension in PHP 5 - Cloning nodes of an XML document with the cloneNode() method
    (Page 4 of 4 )

    As I promised in the section that you just read, the last method that I’m going to teach you in this article will be one that allows you to clone the nodes of a given XML document. Obviously, this process can be extremely useful when it comes to copying nodes within an XML string. Thus, the DOM XML extension offers its handy “cloneNode()” method to perform all of these tasks.

    To demonstrate how the “cloneNode()” method works, I’m going to use it in conjunction with the previous “hasAttribute().” In this particular case, it will be used for cloning some nodes from the “headlines.xml” file that you saw earlier.

    That being explained, here’s the signature of this simple XML file:


    // definition of 'headlines.xml' file


    <?xml version="1.0" encoding="iso-8859-1"?>

    <headlines>

    <headline id="economics">

    <image>image1.jpg</image>

    <url>Link for headline 1 goes here</url>

    <text>Text for headline 1 goes here</text>

    </headline>

    <headline id="sports">

    <image>image2.jpg</image>

    <url>Link for headline 2 goes here</url>

    <text>Text for headline 2 goes here</text>

    </headline>

    <headline id="jetset">

    <image>image3.jpg</image>

    <url>Link for headline 3 goes here</url>

    <text>Text for headline 3 goes here</text>

    </headline>

    <headline id="technology">

    <image>image4.jpg</image>

    <url>Link for headline 4 goes here</url>

    <text>Text for headline 4 goes here</text>

    </headline>

    <headline id="art">

    <image>image5.jpg</image>

    <url>Link for headline 5 goes here</url>

    <text>Text for headline 5 goes here</text>

    </headline>

    </headlines>


    Now that I have listed the complete signature of the “headlines.xml” file, please pay attention to the following hands-on example. It first clones the <headlines> nodes of the file, and then displays the respective values of their ID attributes.

    The corresponding code sample is as follows:


    // example on using the 'hasAttribute()' and 'cloneNode()' methods altogether


    $dom=new DOMDocument();

    // load XML data from existing file

    $dom->load('headlines.xml');

    $headlines=$dom->getElementsByTagName('headline');

    foreach($headlines as $headline){

    // clone node

    $cloneNode=$headline->cloneNode(false);

    if($cloneNode->hasAttribute('id')){

    echo 'ID attribute of current cloned node is the following: '.$cloneNode->getAttribute('id').'<br />';

    }

    }


    As you can see by the above code sample, I used the intuitive “cloneNode()” method to clone all of the <headline> nodes of the previous sample “headlines.xml” file. In this case, I utilized the “hasAttribute()” method that I showed you in the previous section to determine whether the recently-cloned elements have attributes or not. This provides you with a concrete example of how to clone the nodes of an XML document.

    Finally, I recommend that you develop your own set of test examples by using all of the code samples included in this tutorial. The purpose is to improve your skills in using the recently explained method.

    Final thoughts

    That’s all for now. In this sixth chapter of the series, you hopefully expanded your background on the DOM XML library by learning how to work with attributes and cloned nodes of an XML document.

    In the next and last article, I’m going to teach you a couple of additional methods that will come in handy when dealing with child nodes. Now that you’ve been warned about the topics that will be discussed in the final tutorial of the series, you won’t want to miss it!


    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.

       · The DOM XML PHP extension can be used for parsing attributes of a given XML string,...
       · cloneNode() isn't totally intuitive as to not require some explanation. What's the...
       · Thank you for your comments on my PHP article. Concerning your question, you’re...
     

       

    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

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