How to Clone an XML Node using PHPCloning
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.
Note
: We never rent, trade, or sell my email lists to
anyone.
We assure that your privacy is respected
and protected.
_______________________________________
Recommended
XML Books
| |