Hi.
I am a total beginner in JS/XML.
I have this simple code that needs to be extended to list on the screen attributes and their values for each element of a XML file.
function printElement(indent, node)
{
var i;
if (node.nodeType == 3)
{
document.write("<br />" +indent + node.nodeValue);
}
else
{ document.write("<br />" +indent + "[" + node.nodeName + "]");
for (i = 0; i < node.childNodes.length; i++)
{
printElement(indent+tab, node.childNodes[i]);
}
document.write("<br />" +indent + "[/" + node.nodeName + "]");
}
}
I think I am supposed to use node.attributes but I don't know exactly how. I don't know attribute's name.
This also doesn't work:
document.write("<br />" +indent + node.attributes[0].nodeValue);
The browser says "Object required" if (node.nodeType == 3). If (node.nodeType == 2) the code lists something but not the attributes.