View Full Version : Parsing XML nonsense

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


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:

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):


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.

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;


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


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>