Romani
Romani

Reputation: 3241

Best way to read XML in Java

From some of our other application i am getting XML file.

I want to read that XML file node by node and store node values in database for further use.

So, what is the best way/API to read XML file and retrieve node values using Java?

Upvotes: 11

Views: 17364

Answers (8)

Aaron Digulla
Aaron Digulla

Reputation: 328536

There are various tools for that. Today, I prefer two:

Here is a good comparison between the Simple and JAXB: http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-simple.html

Personally, I like Simple a bit better because support by Niall is excellent but JAXB (as explained in the blog post above) can produce better output with less code.

StAX is a more basic API which allows you to read XML documents that simply don't fit into RAM (neither Simple nor JAXB allow you to read an XML document "object by object" - they will always try to load everything into RAM at once).

Upvotes: 7

Wivani
Wivani

Reputation: 2046

Bypassing alltogether the question of parsing the xml and storing the values in a database, I'd like to question the need to do the above. Most databases can handle xml nowadays, so it can be stored in some way into a table without the need of parsing the content; and the content of such an xml within a column in a table can typically be queried by use of 'xmlselect()' and similar functions.

Think about this for a second; if in the near or distant future the content of the xml that you get from the other application changes, you'll have plenty of changes to do. If it changes often, it'll become a nightmare.

Cheers, Wim

Upvotes: 2

forty-two
forty-two

Reputation: 12817

I suggest using XPath. Xalan is already included in the JDK (no external jars needed) and it fits your requirement, i.e. iterating through element nodes (i presume) and storing their text values. For example:

    String xml = "<root> <item>One</item> <item>Two</item> <item>Three</item> </root>";

    XPathFactory xpf = XPathFactory.newInstance();
    InputSource is = new InputSource(new StringReader(xml));
    NodeList nodes = (NodeList) xpf.newXPath().evaluate("/*/*", is,
            XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); ++i) {
        Element e = (Element) nodes.item(i);
        System.out.println(e.getNodeName() + " -> " + e.getTextContent());
    }
}

This example returns a list of all non-root elements and print out the corresponding element name and text content. Adapt the xpath expression to fit your needs.

Upvotes: 4

Liqun
Liqun

Reputation: 4171

well,i used stax to parse quite a huge of XML nodes, which consumes less memory than Dom and sax, cauz it is of style of pulling XML data. Stax might be a good choice for large XML data nodes.

Upvotes: 0

user425367
user425367

Reputation:

I would advice for a simple XML tool if you can manage by that.

For example I and my colleges have introduces complex XML frameworks that worked like a charm at first. Then you forget about the framework, you have special build files just for mapping XML to beans, you have annotated beans, you provide a new barrier for new developers to your project. You loose much of your freedom to refactor.

At the end you will be sorry that you used the complex framework to save some time in the beginning and I have seen more than one time that the frameworks have been thrown out in refactoring because everybody had a negative feeling about it although they are great at paper.

So think twice about introducing complex XML frameworks if you seldom use them. If you and your team use them rather frequently then they are the way to go.

Upvotes: 4

Infeligo
Infeligo

Reputation: 11802

Try XStream, this one's really simple.

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114757

dom4j and jdom are pretty easy to use (ignoring the requirement "best" for a moment ;) )

Upvotes: 1

HJW
HJW

Reputation: 23443

Try Apache Xerces. It is mature and robust. Any such available alternatives will do also, just be sure not to roll out your own implementation.

Upvotes: 2

Related Questions