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

Best way to extract values from xml string?

 
Gerald Luckenbill
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Hopefully this is a really easy question for someone - I am looking for the easiest way to extract the values out of this xml string: I am limited to using Java 1.3. The tag names will always be the same. Is there an existing Java class/api, which will do this - or do I have to just string parse this thing.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<OUTBOUND>
<NAME>Joe Blow</NAME>
<FILENAME>c:\\tmp\\SampleFile.txt</FILENAME>
<ID>1234</ID>
<SUM>23123</SUM>
</OUTBOUND>
To be clear I need to extract, Joe Blow, c:\\tmp\\SampleFile.txt, etc. from this string.
Many Thanks!
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am SAX/DOM challenged, so I can't give you a Java program
to do what you are looking for. Look for any SAX/DOM examples
and you case use the API messages to do what you want.
Thanks.
- madhav.........currently good at XSLT!!!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the "OUTBOUND" element with Document#getDocumentElement(). Then you can access "NAME", "FILENAME", "ID", and "SUM" with Node#getChildNodes(). The value of each element can be obtained using Node#getNodeValue().
 
Gerald Luckenbill
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.
Could you be more specific? What class is this from:
You can get the "OUTBOUND" element with Document#getDocumentElement(). Then you can access "NAME", "FILENAME", "ID", and "SUM" with Node#getChildNodes(). The value of each element can be obtained using Node#getNodeValue().
Any code samples?
Also, from the examples I have seen is there anyway I can use the dom or sax parsers or something else without having an actual xmlFile - my program has an xml string that I am reading off a queue, that I need to parse.
Many Thanks!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class is this from:
Classes org.w3c.dom.Document and org.w3c.dom.Node.
Any code samples?
Take a look at the Java Developer's Almanac.
my program has an xml string that I am reading off a queue, that I need to parse.
See here.
 
Phil Weinstein
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Super cool!
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic