This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Reading/Parsing an XML doc Question 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 » Engineering » XML and Related Technologies
Reply locked New topic
Author

Reading/Parsing an XML doc Question

Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

How about calling selectNodes instead of selectSingleNode? Because the latter selects only a single node -- hence the name.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
Thanks! And then how do I loop through the items now that I amselecting multiple nodes?

Thanks again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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

Joined: Jan 24, 2010
Posts: 72
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
 
Don't get me started about those stupid light bulbs.
 
subject: Reading/Parsing an XML doc Question
 
Similar Threads
Null pointer exception probably due to synchronisation issues; problem in starting activity
xml updations and deletions
Problem with dom4j "Getting started" example
Need Help Iterating Elements Using dom4j
Help regarding how to check the element entered is equal or not in xml file