Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John McClellan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21312
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use the XPath expression "/root/inventory/item" to get a list of those elements.
 
John McClellan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file is:



My code snippet is:



My output is blank. How should I change this code? Thanks!
 
Paul Clapham
Sheriff
Posts: 21312
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21312
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just googled you up a tutorial on how to use XPath in a DOM environment: The Java XPath API.
 
g tsuji
Ranch Hand
Posts: 669
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Am a beginner too.

I thought the whole thing is about understanding what getElementsByTagName expect?
By that I mean what is a TagName?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic