Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing XML with JAXP

 
Rick Crawford
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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 <><
 
Rob Bass
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Xalan to query the DOM Tree
 
Palani kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic