PHP
  Home arrow PHP arrow Page 2 - 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Accessing Attributes and Cloning Nodes with the DOM XML Extension in PHP 5 - Accessing attributes of XML nodes with the getAttribute() method
    (Page 2 of 4 )

    As I said at the beginning, the DOM XML PHP library offers a number of methods that can be used to access attributes of one or more nodes within an XML document. The first of these attribute-related methods that I’m going to show you is named “getAttribute().” It is similar to its client-side incarnation and allows you to retrieve the attribute of a specified element.

    To understand more clearly how this method functions, first I’m going to define a trivial XML file that contains data about some fictional headlines. The structure looks like this:


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


    As you can see, the previous “headlines.xml” file is pretty basic; it’s only composed of a few simple nodes that store information about some sample headlines, including the corresponding titles, thumbnails, and links. Besides, each headline has an ID attribute that helps to differentiate the different categories, such as “sport,” “economics,” “art,” etc. So far, this shouldn't be too complex to grasp.

    Now, with the previous XML file already created, take a look at the example below. It demonstrates how to use the aforementioned “getAttribute()” method to retrieve the respective values of the ID attributes assigned to each document node. Here’s the corresponding code sample:


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


    $dom=new DOMDocument();

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

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

    foreach($headlines as $headline){

    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

    */


    See how easy it is to retrieve the attribute values of certain nodes within an XML document? I bet you do! In this case, I first used the handy “getElementsByTagName()” method to get access to all of the <headline> elements of the document. I then obtained their ID values via the “getAttribute()” method. Short and simple!

    At this point, I’m pretty sure that you've grasped the logic that drives the “getAttribute()” method. Thus, it’s time to learn more features of the DOM XML extension. In the next section I'm going to show you how to use a brand new method that belongs to this PHP library. It's called “has Attribute()” and can be used to determine whether or not a specific XML node has attributes.

    To see the full details of how this brand new method will be implemented, please visit the following section. It’s only one click away.

    More PHP Articles
    More By Alejandro Gervasio


       · 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

    - 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