PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Determining the existence of attributes for a specific XML node with the has Attribute() method
    ( Page 3 of 4 )

    In the course of the previous section, I mentioned that the DOM XML library provides PHP developers with a useful method, called “has Attribute()”, which can be utilized to find out if a specified node of an XML document includes an attribute. Obviously, as you’ll see in a few moments, the implementation of this method is very straightforward. Therefore, I'm going to use the same “headlines.xml” file that you saw in the prior section to demonstrate how to use this method.

    Given that, here’s the corresponding definition of this sample “headlines.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 you've recalled how the above XML file looks, please pay close attention to the following PHP 5 script. It uses the previously-mentioned “has Attribute()” method to determine if each <headline> node of the above “headlines.xml” file contains an attribute or not. If it has one, its value is outputted to the browser:


    // example on using the 'hasAttribute()' method

    $dom=new DOMDocument();

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

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

    foreach($headlines as $headline){

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

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

    }

    }


    /* displays the following

    ID attribute of current node is the following: economics

    ID attribute of current node is the following: sports

    ID attribute of current node is the following: jetset

    ID attribute of current node is the following: technology

    ID attribute of current node is the following: art

    */


    As demonstrated above, once the contents of the “headlines.xml” file have been read via the already familiar “load()” method, they’re parsed with a simple “foreach” PHP loop. Naturally, the most relevant thing to stress here is how the “has Attribute()” method is used to determine if each <headline> node contained in the file has an ID attribute.

    If this last condition is true, then the respective ID's values are simply echoed to the browser. Now don’t say that this method wasn’t simple to grasp!

    So far, everything looks good. At this time you should feel pretty satisfied, since you learned how to use a couple of methods included with the DOM XML extension to find out whether certain nodes of a given XML document have attributes, as well as to retrieve the values of the attributes in question.

    Nevertheless, this educational journey exploring the main features of the DOM XML extension isn't finished yet. There are other useful methods that deserve a closer analysis. Therefore, the last section of this tutorial will be focused on the library’s capability to clone the nodes of a specified XML document.

    Sounds really interesting, right? But, to learn how to work with clone nodes, you’ll have to click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek