File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion 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: 19781
    
  20

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: 19781
    
  20

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading/Parsing an XML doc Question