All,
Can someone help me out? I'm trying to extract information from child nodes within an XML document.
Ideally, we want to locate a base item in the XML document and then retrieve information on the extended item data. Extended items should be child data of the base item. When I try to run the code below in a browser (FireFox 6.0.2), I can't understand some of the information that is displayed about the XML data.
My specific questions are also annotated in the code:
1. Why is the child length of documentElement 7? I would expect it to either be 3 (the number of child nodes) or 15 (if the length should include next-level child nodes)
2. If I'm trying to retrieve information about the first-level nodes, why do I need to use odd numbers ([1] returns the first node, [3] returns the second node, etc).
3. If I iterate through the first level of nodes, why are there a bunch of empty text nodes? As far as I can tell from the XML source, there aren't any text nodes, just attribute and element nodes
4. I don't undestand why I cannot get the attribute information. The code doesn't work - do I need to retrieve a child of extitem to get attribute information?
Thanks for any help that you can provide.
-Dan
<html>
<head>
<title></title>
<script id="itemlist" type="application/xml">
<itemlist>
<baseitem basepartno="00001">
<extitem extpartno="000001-a" maxlen="24" />
<extitem extpartno="000001-b" maxlen="48" />
<extitem extpartno="000001-c" maxlen="72" />
<extitem extpartno="000001-d" maxlen="72" />
</baseitem>
<baseitem basepartno="00002">
<extitem extpartno="000002-a" maxlen="24" />
<extitem extpartno="000002-b" maxlen="48" />
<extitem extpartno="000002-c" maxlen="72" />
<extitem extpartno="000002-d" maxlen="48" />
<extitem extpartno="000002-e" maxlen="72" />
</baseitem>
<baseitem basepartno="00003">
<extitem extpartno="000003-a" maxlen="24" />
<extitem extpartno="000003-b" maxlen="48" />
<extitem extpartno="000003-c" maxlen="72" />
</baseitem>
</itemlist>
</script>
<script type="text/javascript">
var xmlDoc;
function loadxml() {
var orderSource = document.getElementById("itemlist").textContent;
var parser = new DOMParser();
xmlDoc = parser.parseFromString(orderSource, "application/xml");
jsv_emp = xmlDoc.documentElement.childNodes;
document.write("XML Root Tag Name: " + xmlDoc.documentElement.tagName);
document.write("<br />");
//Question 1: Why is this seven?
document.write("childNode length = " + jsv_emp.length); //Don't understand why this is 7
document.write("<br />");
//Question 2: Why does jsv_emp[] only work for odd numbered records?
//This displays 00001
document.write("childNode[1] = " + jsv_emp[1].attributes[0].nodeValue);
document.write("<br />");
//This displays 00002
document.write("childNode[3] = " + jsv_emp[3].attributes[0].nodeValue); //Why is this the third record?
document.write("<br />");
//Trying to get the child nodes of partno 00002
x=xmlDoc.getElementsByTagName("baseitem")[1].childNodes;
y=xmlDoc.getElementsByTagName("baseitem")[1].firstChild;
document.write(x.length);
document.write("<br />");
document.write("-------------- <br />");
for (i=0;i<x.length;i++)
{
//Question 3: Why does this display a bunch of empty text nodes? I don't think there are any text nodes in the XML (above)
document.write("Node Name: " + y.nodeName + " Node Type: " + y.nodeType + " Node Value: " + y.nodeValue + "<br />");
//Question 4: Why doesn't this work?
//document.write("extitem: " + y.attributes[0].nodeValue + "<br />");
y=y.nextSibling;
}
document.write("-------------- <br />");
document.write("<br />");
document.write("<br />");
document.write("Done");
}
</script>
</head>
<body onLoad="javascript:loadxml();">
</body>
</html>