tags:

views:

442

answers:

6

I need to read an XML file using java.Its contents are something like

<ReadingFile>
<csvFile>
<fileName>C:/Input.csv</fileName>
<delimiter>COMMA</delimiter>
<tableFieldNamesList>COMPANYNAME|PRODUCTNAME|PRICE</tableFieldNamesList>
<fieldProcessorDescriptorSize>20|20|20</fieldProcessorDescriptorSize>
<fieldName>company_name|product_name|price</fieldName>
</csvFile>
</ReadingFile>

Is there any special reader/jars or should we read using FileInputStream?

+4  A: 

Check out Java's JAXP APIs which come as standard. You can read the XML in from the file into a DOM (object model), or as SAX - a series of events (your code will receive an event for each start-of-element, end-of-element etc.). For both DOM and SAX, I would look at an API tutorial to get started.

Alternatively, you may find JDOM easier/more intuitive to use.

Brian Agnew
A: 

xstream would do very nicely here. Check out the one page tutorial

Ryan Fernandes
A: 

There are two major ways to parse XML with Java. The first is to use a SAX parser see here

which is fairly simple.

The second option is to use a DOM parser see here

which is more complicated but gives you more control.

Peter
A: 

JAXB is another technology that might suit your needs.

A: 

Another suggestion: Try out Commons digester. This allows you to develop parsing code very quickly using a rule-based approach. There's a tutorial here and the library is available here

I also agree with Brian and Alzoid in that JAXB is great to get you up and running quickly. You can use the xjc binding compiler that ships with the JDK to auto generate your Java classes given an XML schema.

Adamski