PHP
  Home arrow PHP arrow Page 4 - Accessing Attributes and Cloning Nodes with the DOM XML 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? 
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: starstarstarstarstar / 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:
      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


    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!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT