File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes GWT and the fly likes How do loop through an XML file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » GWT
Bookmark "How do loop through an XML file?" Watch "How do loop through an XML file?" New topic

How do loop through an XML file?

Laurence Turpin

Joined: May 21, 2012
Posts: 19

I'm using GWT 2.4 on Windows 7
I'm trying to parse the following XML file:

The problem is there are 3 session nodes and I need have a loop in my program.
so it is:

While session
parse session
get next session

but I don't know how to do the loop.
I have tried to do it with a for loop but it doesn't work correctly.
I get the first session data 3 times.
When what I want is all of the 3 different session information.

Here is the program I have so far:

The for loop is where the problem is

Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

At line 69 you get the first of the three <session> elements. You do that every time through the loop.

Why don't you just get all of the <session> elements and loop through them? (Hint: Hard-coding the number "3" is both unnecessary and wrong.)
Laurence Turpin

Joined: May 21, 2012
Posts: 19
Thank you Paul Clapham for replying I now have a program that works.
I replaced line 69 with :

Node sessionNode = trainingLogDom.getElementsByTagName("session").item(i);

I replaced item(i) where ever item(0) occurred in my program.

It now works
However I still have one query.
Suppose that the number of sessions were 100s or more.
They would be too many to count.
What I really need to be able to do is parse all the session elements without know how many there are?

The basic structure of the loop needs to be:
While session elements to parse
parse session element
get next session element

Below is the revised program I now have

Laurence Turpin

Joined: May 21, 2012
Posts: 19
Finally managed to work out how to do the while loop.
Here is the solution shown below.

It is sorta covered in the JavaRanch Style Guide.
subject: How do loop through an XML file?
Similar Threads
Can't read an XML file.
Need Help
help regarding createing node in xml file
servlets and xml