homepage Welcome to WebmasterWorld Guest from 54.226.252.142
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Question about navigating XML with JavaScript
Trying to extract information from child nodes within an XML document
mogulmaniac




msg:4364893
 5:39 pm on Sep 20, 2011 (gmt 0)

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>

 

httpwebwitch




msg:4364916
 6:32 pm on Sep 20, 2011 (gmt 0)

Why is the child length of documentElement 7?


the JS parser is turning whitespace between elements into text nodes.

(whitespace)
<node>
(whitespace)
<node>
(whitespace)
<node>
(whitespace)

that makes 7 children

why do I need to use odd numbers


same reason.

why are there a bunch of empty text nodes


remove the spaces and they'll disappear.

when looping through your nodes, output node.attributes.length, to see if the node has any attributes. Then only if the length > 0 should you try to access node.attributes[0].nodeValue

httpwebwitch




msg:4364917
 6:33 pm on Sep 20, 2011 (gmt 0)

Welcome to WebmasterWorld, mogulmaniac!

mogulmaniac




msg:4364934
 7:38 pm on Sep 20, 2011 (gmt 0)

httpwebwitch -

Thanks for the tip. Removing the whitespace and checking the length of node.attributes solved all of the problems.

Much appreciated!

-Dan

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved