Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying one DOM XML node to another in JavaScript

I wrote a simple function to copy contents(including all child nodes) from one node to another of an XML DOM Object.

Surprisingly in the function, the for loop is not iterating for n=1 for some reason i cant figure out, please help, i've been trying to debug this in all possible ways from 4 days.

This function is not iterating for n=1 (i.e. it's iterating for n=0 and n=2 and so on except n=1):

function copyNode(fromNode, toNode) {

 for (var n=0; n [lessthan] fromNode.childNodes.length; n++) {
   var fromChild = fromNode.childNodes[n];
   var toChild = toNode.appendChild(fromChild);
   copyNode(fromChild, toChild);
 }
}

Full code:

<html>
<head>


<script type="text/javascript">

//STANDARD TEXT to XMLDOM OBJECT RETURN FUNCTION
function getXmlDoc(text){
var xmldoc = null;
try //Internet Explorer
{
xmldoc=new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async="false";
xmldoc.loadXML(text);
return xmldoc;
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
parser=new DOMParser();
xmldoc=parser.parseFromString(text,"text/xml");
return xmldoc;
}
catch(e)
{
alert(e.message);
return;
}
}
}
//END OF getXmlDoc

//Function that refuses to work, the 2nd child is always being skipped, i.e n=1 is never running :(, ONLY n=1 is not running
function copyNode(fromNode, toNode) {

 for (var n=0; n [lessthan] fromNode.childNodes.length; n++) {
   var fromChild = fromNode.childNodes[n];
   var toChild = toNode.appendChild(fromChild);
   copyNode(fromChild, toChild);
 }
}

//Function to clear a node's contents
function clearNode(node2)
{
x2=node2.childNodes;
if(x2.length!=0)
{
for(i=0;i [lessthan] x2.length;i++)
{
    node2.removeChild(x2[i]);
}
}
}

//XML1
text="<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";

xmlDoc=getXmlDoc(text);

//XML2
text2="<book>";
text2=text2+"<title><a>1</a><b>2</b><c><ca>3</ca></c></title>";
text2=text2+"<year>2006</year>";
text2=text2+"</book>";

xmlDoc2=getXmlDoc(text2);

x=xmlDoc.documentElement.childNodes;
y=xmlDoc2.documentElement.childNodes;
var string = (new XMLSerializer()).serializeToString(y[0]);
alert(string);
var string = (new XMLSerializer()).serializeToString(x[0]);
alert(string);
clearNode(x[0]);
copyNode(y[0],x[0]);
var string = (new XMLSerializer()).serializeToString(xmlDoc);
alert(string);
</script>
</head>
<body>
</body>
</html>
like image 844
smhx Avatar asked Feb 26 '26 20:02

smhx


1 Answers

As you move (not copy) the source nodes to the destination, they are removed from the list of childNodes on the source, and its length decreases.

You should use something like:

while (fromNode.firstChild) {
    toNode.appendChild(fromNode.firstChild);
}

instead.

Also, you don't need the recursion; when the node is moved, all its children will be moved with it.

like image 57
NickFitz Avatar answered Mar 01 '26 09:03

NickFitz