File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Need Help Iterating Elements Using dom4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need Help Iterating Elements Using dom4j" Watch "Need Help Iterating Elements Using dom4j" New topic

Need Help Iterating Elements Using dom4j

Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
I would like to iterate through and print all the <item><description> elements in an XML doc. My code (below) only returns the first <item><description> element. How do I get them all?

Thank you,

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I don't know dom4j, but that line 49 looks fishy to me. You already got nodes which matched /rss/channel/item/description, so once you have them you shouldn't have to provide any XPath expression to get them. Or whatever you meant that to be.

My guess is that the //user//description string is being used as an XPath expression. So it returns all of the nodes which you already got earlier in the code, and then the valueOf() method necessarily selects just the first of them.

But I'm just guessing. I would say you don't need that line of code.
I agree. Here's the link:
subject: Need Help Iterating Elements Using dom4j
It's not a secret anymore!