aspose file tools*
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
JavaRanch » Java Forums » Frameworks » GWT
Bookmark "How do loop through an XML file?" Watch "How do loop through an XML file?" New topic
Author

How do loop through an XML file?

Laurence Turpin
Greenhorn

Joined: May 21, 2012
Posts: 19
Hello

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
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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
Greenhorn

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
Greenhorn

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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do loop through an XML file?