• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to retrieve data from XML document using JAVA APIs  RSS feed

Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!