How to Clone an XML Node using PHP

Cloning an XML node is useful if you want to copy an existing xml node and then make the necessary changes to it to make it a unique one. To do this task PHP provides you with a cloneNode function that can be used to clone an existing node.


_______________________________________________


Consider an XML file candidate.xml that has the code as below:

<?xml version="1.0" encoding="UTF-8"?>
<candidate>
<candidatename id='candid'>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
</candidate>
<candidate>
<candidatename id='candid1'>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>LMN Corp</organization>
</candidate>

If you look at the above XML file you will find that the candidate node can be cloned and then easily modified to include another candidate. So, in this example we will be cloning the first node which has the attribute "id" as "candid". The code that performs this action is given below:

<?php
$xdoc = new DOMDocument;
$xdoc->Load('C:/php/xmlfiles/candidate.xml');
$organization = $xdoc->getElementsByTagName('organization')->item(0);
$xp = new domXPath($xdoc);
$qry = "//candidate";
$xpQry = $xp->query($qry);

$size = $xpQry->length;

for ($i=0; $i<$size; $i++){
$nd = $xpQry->item($i);
if($i==0){
$cn = $nd->cloneNode(true);
}else{
$cn = $nd->cloneNode(false);
}

$cn->setAttribute('cloned',$i);
$organization->appendChild($cn);

echo "<HTML><Head>";
echo "<title>Cloning a Node</title>";
echo "</Head><body><B>";

echohtmlentities($xdoc ->saveXML());

echo "</B></body></HTML>";
?>

In the above code a new DOMDocument is added and then the XML file is added to it. In the above example the variables $xp is used to store the xpath, $qry is used to store the query, and the $xpQry is used to store the xpathquery. We use a 'for loop' to clone the node. In that we do two type of cloning. One is the deep cloning where you get all the elements of the node and the other is the shallow cloning where we get only the node. We set an attribute named "cloned" to identify whether that node is cloned or not.

The result of the above code will give you an XML file whose content will be as follows:

<?xml version="1.0" encoding="UTF-8"?>
<candidate>
<candidatename id='candid'>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
</candidate>
<candidate>
<candidatename id='candid1'>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>LMN Corp</organization>
</candidate>
<candidate cloned="0">
<candidatename id='candid'>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
</candidate>
<candidate cloned="1" />

Thus you can use cloning to duplicate a particular node in an XML file.



_______________________________________________

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

Copyright - © 2004 - 2017 - All Rights Reserved.