Some code I put together to import some XML retrieved via AJAX into a document (stored here so I can find it again in the future).
IE won’t let you import a cloned nodeset into a document, so I wrote this for my UniView utility. The code starts with a node in the AJAX data and creates a copy of all elements and attributes in the current document.
function copyNodes (ajaxnode, copiednode) {
for (var node=ajaxnode.firstChild; node != null; node = node.nextSibling) {
if (node.nodeType == 3){ //text
copiednode.appendChild(document.createTextNode(node.data));
}
if (node.nodeType == 1){ //element
var subnode = document.createElement(node.nodeName);
var attlist = node.attributes;
if (attlist != null) {
for (var i=0; i<attlist.length; i++){
subnode.setAttribute(attlist[i].name, attlist[i].value);
}
}
copiednode.appendChild(subnode);
copyNodes(node, subnode);
}
}
}
It doesn’t expect processing instructions, comments etc. Just elements and attributes. (Though of course that can be added, if needed.)
