Obtain the contents of an XML element and convert it to text. This script is great for using with Ajax-type applications, when it’s necessary to send back the contents of an element to the server without knowing what’s there.
/* This script and many more are available free online at
The JavaScript Source :: http://javascriptsource.com
Created by: Neal Venditto :: http://neal.venditto.org/ */
function getXML(aNode) {
var out = ”;
if(aNode.nodeType == Node.ELEMENT_NODE) {
out = ‘<‘+aNode.nodeName;
if(aNode.hasAttributes()) {
var atts = aNode.attributes;
for(var x=0;x<atts.length;x++) {
out +=’ ‘+atts[x].nodeName+’=”‘+atts[x].nodeValue+'”‘;
}
}
if(aNode.hasChildNodes()) {
out += ‘>’;
var kids = aNode.childNodes;
for(var x=0;x<kids.length;x++) {
switch(kids[x].nodeType) {
case Node.ELEMENT_NODE:
out += getXML(kids[x]);
break;
case Node.TEXT_NODE:
out += kids[x].nodeValue;
break;
case Node.COMMENT_NODE:
out += ‘<!–‘+kids[x].nodeValue+’–>’;
break;
case Node.CDATA_SECTION_NODE:
out += ‘<‘+’![CDATA[‘+kids[x].nodeValue+’]’+’]>’;
break;
}
}
out += ‘</’+aNode.nodeName+’>’;
} else {
out += ‘/>’;
}
}
return out;
}