• 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

Parsing XML with JAXP

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all. Could someone kindly post some code to show me how to parse the following example XML data (read in from a string?)
<?xml version="1.0" ?>
<FORMDATA>
<BILLING>
<INVOICENO>1111111</INVOICENO>
<CUSTOMERNO>ABC123</CUSTOMERNO>
</BILLING>
</FORMDATA>
I'm using the JAXP parser. The document seems to be parsing okay, as I can System.out.println doc.getDocumentElement() and see the entire XML stream.
Here's the code I've got so far, which isn't much:

I'm after the value of the INVOICENO and CUSTOMERNO. Is there some sort of way to access the value of these elements by XML tag names?
Thanks in advance for the help,
Rick <><
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Xalan to query the DOM Tree
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hei
after parsing you can take the values by getting to the node
you can take this by using the method
NodeList nl = Document.getElementsByTagName("INVOICENO");
as shown above this gives all the elements with the said name.
Node invoice= nl.item(0); \\this gives you the code the value '0' denotes the first element
Node value = invoice.getFirstChild(); \\this gives the text node from where you can take the value
String s = value.getNodeValue();
Hope this helps
Cheers
:-)
Palanikumar
------------------
In the end everything is Right,If it's not then It's not the End
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic