• 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

getting nested xml tags of the same name

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm using jaxp from sun for my xml parsing. i have problems obtaining the value of a tag with duplicate names but residing on different level. e.g.,
<Customer>
<CustomerID>Experts Exchange</CustomerID>
<Order>
<Item>1</Item>
<CurrencyCode>USD</CurrencyCode>
<Amount>94</Amount>
<Order>
<CreditLimit>
<CurrencyCode>USD</CurrencyCode>
<Amount>4000</Amount>
</CreditLimit>
<Product>
<ProductItem>
<ProductName>Peanuts</ProductName>
<CurrencyCode>USD</CurrencyCode>
<Amount>4.99</Amount>
</ProductItem>
<ProductItem>
<ProductName>Sweets</ProductName>
<CurrencyCode>USD</CurrencyCode>
<Amount>15.99</Amount>
</ProductItem>
</Product>
suppose now i want to obtain the value of 'CurrencyCode' at different levels using DOM, how do i actually code for it? any sample codes are appreciated.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can call the getElementsByTagName(java.lang.String tagname) method on Document object returned by the parser.
The method takes the tag name as the argument and returns a NodeList of all the Elements with the given tag name in the order in which they are encountered in a preorder traversal of the Document tree.
In your case, the argument will be "CurrencyCode".
Once you have the list, you can iterate through it, retrieve individual objects and process them.
Hope that helps!
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your name ervinloh does not comply with the JavaRanch naming policy. We require the displayed names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please spare a moment and modify your profile to use a publicly displayed name that meets these requirements.
Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic