File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Best way to extract values from xml string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Best way to extract values from xml string?" Watch "Best way to extract values from xml string?" New topic
Author

Best way to extract values from xml string?

Gerald Luckenbill
Greenhorn

Joined: Nov 05, 2003
Posts: 15
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

Joined: Jun 03, 2000
Posts: 5040
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!!!


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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().


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gerald Luckenbill
Greenhorn

Joined: Nov 05, 2003
Posts: 15
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Nov 30, 2003
Posts: 5
Super cool!
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to extract values from xml string?
 
Similar Threads
Regarding xml Data
Representing an xml
The system cannot find the path specified
Read a .xls,.xlsx file format using XSSF
join list of strings