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.
<?xml version="1.0" encoding="UTF-8"?> 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 $size = $xpQry->length; for ($i=0; $i<$size; $i++){ $cn->setAttribute('cloned',$i); echo "<HTML><Head>"; 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"?> Thus you can use cloning to duplicate a particular node in an XML file.
|
|
|||
|
||||
“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”
Copyright - © 2004 - 2019 - All Rights Reserved.