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

  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



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"?>


<headline id="economics">


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

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


<headline id="sports">


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

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


<headline id="jetset">


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

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


<headline id="technology">


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

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


<headline id="art">


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

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



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();



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


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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