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

Cloning nodes of an XML document with the cloneNode() 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 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!



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