I have the following JavaScript code to "display" XML on a website:
function createChild(node,tabindex){
var child = node.childNodes;
var r = '';
var tabs = '';
for(i=0;i<tabindex;i++){
tabs += "\t";
};
for(i=0;i<child.length;i++){
if(child[i].nodeType == 1){
r += tabs+"<"+child[i].nodeName+">\n";
if(child[i].hasChildNodes()){ r += createChild(child[i],1); }; // here is where it fails!
r += tabs+"</"+child[i].nodeName+">\n";
}
}
return r;
};
function parseXML(xml){
var doc = new DOMParser().parseFromString(xml,'application/xml');
var node = doc.getElementsByTagName('*')[0];
var r = '';
r += "<<span class=\"highlight highlight-blue\">"+node.nodeName+"</span>>\n";
if(node.hasChildNodes()){ r += createChild(node,1); };
r += "<<span class=\"highlight highlight-blue\">/"+node.nodeName+"</span>>\n";
$('.viewer').html(r);
};
This works fine on XML like this:
<properties>
<property></property>
</properties>
But it fails to work when there is more than one child like this:
<properties>
<property>
<value></value>
</property>
</properties>
The code keeps running until my browser crashes. I think there is an infinite loop in it, but I'm not sure. Does anyone have a tip for me?