Home arrow PHP arrow Page 2 - Accessing Attributes and Cloning Nodes with the DOM XML Extension in PHP 5

Accessing attributes of XML nodes with the getAttribute() method - PHP

You've probably realized that the DOM XML library can be pretty useful when working with web documents. However, this extension includes some other methods, which can be utilized to parse attributes of a given XML string, as well as to clone nodes. Thus, considering that you may want to learn the basics of how to use them, in this sixth part of the series I’ll be taking a closer look at them by way of some approachable, yet educational, code samples.

TABLE OF CONTENTS:
  1. Accessing Attributes and Cloning Nodes with the DOM XML Extension in PHP 5
  2. Accessing attributes of XML nodes with the getAttribute() method
  3. Determining the existence of attributes for a specific XML node with the has Attribute() method
  4. Cloning nodes of an XML document with the cloneNode() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
April 01, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: