Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
Marshal
Posts: 25176
64
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.

 
Everybody! Do the Funky Monkey! Like this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!