2

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?

1
  • @Harish - XML won't render/display properly unless you use the 'code' button to display it (just select the text and hit the button looking like 0101) Commented Jun 30, 2009 at 10:47

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

0

You can user external libraries like Castor https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1046622.html I have used castor in past. Here are few other links that might help. http://www.xml-training-guide.com/e-xml27.html

http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/XMLReader.html http://www.cafeconleche.org/books/xmljava/chapters/ch07.html

Comments

0

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.

Comments

0

JAXB is another technology that might suit your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.