• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 734
7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic