This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
    Bookmark Topic Watch Topic
  • New Topic

Reading/Parsing an XML doc Question

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,
I have the following class which successfully reads and parses an XML doc. The problem is, it only reads the first <item> in the list.

What do I need to do in order to loop through the doc so that I can read the first few (or all) of the items? Here is my code:

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
How about calling selectNodes instead of selectSingleNode? Because the latter selects only a single node -- hence the name.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks! And then how do I loop through the items now that I amselecting multiple nodes?

Thanks again.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
selectNodes returns a java.util.List; I'm sure I don't need to tell you how to iterate over a List. Because the List isn't generic the only thing you'll need to do is cast the elements to Node; but you need to do that for selectSingleNode too.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks for the help so far.

I am VERY new to XML (and sort of new to Java).

It seems I am still only getting the first item. Does this look right?


I would like to print all the <item><description> elements in the entire document.

Thank you,
-Russ
 
    Bookmark Topic Watch Topic
  • New Topic