Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML with Javascript

Status
Not open for further replies.
Nov 29, 2007
5
US
I am having a hard time parsing XML with Javascript. The XML is an AJAX response. What I am trying to do is get the tagName and place that in one variable, and then get the value and place that in another variable.

Firefox is giving me fits, reporting the length of the document to be almost twice as long as it really is, and will tell me that a childNode[x] has no data UNLESS the childNode[x] is an odd number.

I just want to parse an xml file for the tagName's and values.

Any help would be greatly appreciated.

gkc
 
When I first started using XML as the medium for replies to AJAX queries, I thought I had a real can of worms, too. But a close look at the DOM produced when Firefox parses your XML for you will reveal that every time the XML contains a line-break, the DOM gets a text node (nodeType=3). Unless you are doing something particularly odd, you ought to be able to walk the DOM tree and find Element nodes (nodeType=1) which have non-empty child text nodes. When you find such a combination, the element node will have a tagName, and the child (text) node will have a nodeValue. These should be what you need. And the good news is that IE and Safari will yield the same results.

BTW, the O'Reilly JavaScript book tells all about this, but it takes quite a reading of it, since you need to absorb the notion of an inheritance tree: node -> Element -> HTMLElement, and some of the properties are possessed by all nodetypes, some only by some of the subtypes.
 
[1]
>Firefox is giving me fits, reporting the length of the document to be almost twice as long as it really is, and will tell me that a childNode[x] has no data UNLESS the childNode[x] is an odd number.
It is because ff's parser engine default setting would not ignore insignificant whitespace and take for instance formating carriage-return and line-feed as text node, whereas ie parser default engine setting would ignore insignificant whitespaces text node.
[2]
>I just want to parse an xml file for the tagName's and values.
What is the contact point? Suppose the elem be container (element) node and you want to find its immediate child nodes' tagName (if defined) and its value (be careful, it may not be defined unless it is of simple type), it is something like this.
[tt]
var onode, stagname, svalue;
for (var i=0; i<elem.childNodes.length;i++) {
onode=elem.childNodes;
if (onode.nodeType==1) {
stagname=onode.tagName;
//snodevalue is meaningful if only onode is simple
//else you have to probe its descendants
svalue=onode.firstChild.nodeValue;
alert(stagname+"\n"+svalue); //for illustration
//if you have found what you want break out with statement "break"
//insert conditional checking here
}
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top