...

View Full Version : Parsing XML nonsense



moejoe22
12-15-2008, 04:08 AM
I have an xml like this..

<system>
<lot1>data1</lot1>
<lot2>data2</lot2>
<lot3>data3</lot3>
.
.
.
<lotn>datan</lotn></system>

And I am having the hardest time retrieving the data using DOM. The n is undetermined and can take on any value greater than or equal to 1.

var x=xmlDoc.getElementsByTagName("system");
document.writeln( x[0].childNodes[0].nodeValue );

that line should write data1 to the screen, but instead writes a big blank whitespace. :mad:

gsnedders
12-15-2008, 10:08 PM
The first child of the system element in that example is a text node, containing a line break. As such, look at
document.writeln( x[0].childNodes[1].nodeValue );

rnd me
12-15-2008, 10:32 PM
the following syntax is a little more robust (it ignore text nodes):


x[0].getElementsByTagName("lot1")
x[0].getElementsByTagName("lot2")
x[0].getElementsByTagName("lot3")

moejoe22
12-16-2008, 04:52 AM
I thought about getting tag element by name.. but the number of lots is undetermined. When would you stop? How could you know the number of them. x[0].length returns 1.

A1ien51
12-16-2008, 03:17 PM
Play with this:


var kid = myNode.parentNode.nextSibling;

//Make sure we do not get a whitespace node
while(kid != null && kid.nodeType != 1){
kid = kid.nextSibling;
}


Eric

Kor
12-17-2008, 03:37 PM
Your XML is not quite well formatted. What about?:


<system>
<lot>data1</lot>
<lot>data2</lot>
<lot>data3</lot>
.
.
.
<lot>datan</lot>
</system>

There is no reason for you should use <lotn> tags within an XML file. Anyway, you should not need that indentation, as long as the text value datan should be, in fact, what you are interested in.

But even if you might need that, you have 2 possibilities:

1. using a while loop
2. using attributes
<lot val="1">data1</lot>
<lot val="2">data2</lot>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum