• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How do loop through an XML file?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally managed to work out how to do the while loop.
Here is the solution shown below.

 
Get me the mayor's office! I need to tell her about this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!