Accessing Attributes and Cloning Nodes with the DOM XML Extension in PHP 5

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.

One of the things that has made PHP so popular with web developers is its easy learning curve, along with its flexibility, particularly when it comes to building web applications that cover a huge variety of needs. This versatility becomes evident when working with XML documents, since PHP comes equipped with a decent number of built-in extensions that allow it to handle XML data in all sorts of clever ways without putting you through a painful experience.

Naturally, it’s impossible to cover all of these XML-related extensions in only one series of articles, and it’s certainly not my intention to do that. However, as you might have guessed, the objective of this set of tutorials is to teach you how to use the most relevant methods that come bundled with one of these helpful extensions, “DOM XML.” It permits you to work with XML documents in a relatively easy way using the API provided by the Document Object Model.

Therefore, if you want to learn how to put this useful XML handling library to work for you with minor hassles, I recommend you start reading this article series. Hopefully by the end of it, you’ll be armed with a solid background in using the methods provided by the DOM API, which processes XML documents without making you suffer chronic headaches.

Now that I’ve introduced you to the main subject of this series, it’s time to recapitulate the themes that were covered in the last article. You’ll possibly recall that in the course of the preceding tutorial, I explained how to use a few helpful methods that come bundled with the DOM XML extension, “loadHTMLFile()”, “saveHTMLFile()”, and “loadHTML()” respectively, to perform some basic operations with HTML documents, such as saving them to a destination file, reading their contents from a specified file location, and so forth.

So, are you ready to learn how to work with attributes and child nodes using the DOM XML extension? Let’s get started!

{mospagebreak title=Accessing attributes of XML nodes with the getAttribute() method}

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.

{mospagebreak title=Determining the existence of attributes for a specific XML node with the has Attribute() method}

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.

{mospagebreak title=Cloning nodes of an XML document with the cloneNode() method}

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!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan