It's not a secret anymore!*
The moose likes XML and Related Technologies and the fly likes How do I use DOM to gather information on an XML file with repeating child elements? 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 "How do I use DOM to gather information on an XML file with repeating child elements?" Watch "How do I use DOM to gather information on an XML file with repeating child elements?" New topic
Author

How do I use DOM to gather information on an XML file with repeating child elements?

John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
Hello. I've looked around some and haven't found a good tutorial on this. I've got an XML file in the format of:



The <item> element is repeating and optional, and I don't know in general how many there will be. However I need to use DOM to go through and gather each of the strings located in the <item> elements. However this should only be done in <item> elements within the <inventory> tag, not in any of the other elements. A lot of the tutorials don't cover how to work with repeating elements and are low-quality, so that's why I'm asking this question here. What's a really simple and easy-to-understand way to do this? If you know of a good tutorial that covers dealing with repeating, nested elements, please link it. Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I would use the XPath expression "/root/inventory/item" to get a list of those elements.
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
The file is:



My code snippet is:



My output is blank. How should I change this code? Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You should change it to use XPath. You are selecting zero records because there aren't any elements with that name, which in any case isn't even a valid XML element name.
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
How so? I thought that was what I was doing when placed the "/root/inventory/item" in there, instead of just a tag name. I'm just starting out on parsing XML in Java, so what does my code snippet need to be changed to?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I just googled you up a tutorial on how to use XPath in a DOM environment: The Java XPath API.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 511
    
    3
However this should only be done in <item> elements within the <inventory> tag, not in any of the other elements.


Its scope is meant more generally to have item being "descendant" of inventory rather than only an immediate child element of it. If you want strictly immediate child only, try your own know-how based on the above.>
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
Thank you! That worked excellently. I'm not using any grandchildren inside the inventory element, so that'll work fine. As for the links earlier, sorry, I thought that was just this site looking at keywords in your message and adding stuff in automatically.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 416
    
    4




Am a beginner too.

I thought the whole thing is about understanding what getElementsByTagName expect?
By that I mean what is a TagName?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I use DOM to gather information on an XML file with repeating child elements?