Two Laptop Bag
The moose likes XML and Related Technologies and the fly likes How to retrieve data from XML document using JAVA APIs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to retrieve data from XML document using JAVA APIs" Watch "How to retrieve data from XML document using JAVA APIs" New topic

How to retrieve data from XML document using JAVA APIs

Sweety Anthony

Joined: Jan 17, 2001
Posts: 5
I have downloaded xerces.jar and xml4.jar and set the classpath right but still I just dont know which APIs to use extract the data .
The following is the xml input I receive as a string:
<?xml version='1.0' encoding='ISO-8859-1' ?>
<headlines code='POL' language=''>
<headline storyid='979637107nL1657083' date='16 Jan 2001' time='14:55' lang='EN'>
<text>UPDATE 1-Germany's Fischer faces up to radical past in court</text>
</headline><next_headline storyid='979618902nL16460686'/> </headlines>
Now from this I want to extract the values of storyid date etc.
Do I need to write a DTD for this ?
I dont want to validate this XML document.
Ajith -- I did download and set the classpath . I tried using the class InputSource and AttributesImpl and their methods but it always gives me null values.
I feel really lost please help.
I work on linux.

Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
A lot of times an XSL can do the extraction job much easier and faster than an XML. The downside of using XSL is that, you will not be able to process "fragments" of the XML data and/or feed them to a Java program.
What I'm trying to say is that, if you just need the display the id( or for that matter any specific part(s) of the XML ), try to write an XSL. If you're requirement is more complicated than that, like doing a DB-lookup based on the ID etc, then you will need a parser interface.
If you don't need validation, you don't have to use a DTD. So to begin with just use plain parsing. You will have to do the following -

  • Implement ContentHandler interface.
  • Write a class that instantiates the parser.
  • Set the content handler to your custom content handler.
  • Pass the XML file to the parser as an URI.
  • You will need to watchout for the startElement() and endElement() callbacks, and then do the id based lookup from attribute list.

  • Ofcourse this is the SAX route. You could even do the DOM root.
    If you need examples, try the Java and XML book by O'Reilly.
    Ajith Kallambella M.
    Sun Certified Programmer for the Java2 Platform.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: How to retrieve data from XML document using JAVA APIs
It's not a secret anymore!