Best way to read XML in Java

From some of our other applications, I get an XML file.

I want to read this node XML file by node and store the node values ​​in the database for future reference.

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

+8
java xml
source share
8 answers

dom4j and jdom are pretty easy to use (ignoring the β€œbest” requirement for a moment;))

+2
source share

There are various tools for this. Today I prefer two:

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

Personally, I like Simple a little better, because Niall support is great, but JAXB (as explained in the blog post above) can improve the result with less code.

StAX is a simpler API that allows you to read XML documents that simply do not 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) .

+7
source share

I would advise for a simple XML tool if you can handle this.

For example, my colleges and I introduce sophisticated XML frameworks that initially worked like a charm. Then you forget about the structure, you have special assembly files, intended only for mapping XML to beans, you annotated beans, you provide a new barrier for new developers for your project. You lose most of your freedom to refactor.

In the end, you will be sorry that you used a complex structure to save time in the beginning, and I saw more than once when the frameworks were thrown into refactoring because everyone had a negative feeling about it, although they work fine on paper.

So think twice about implementing complex XML frameworks if you rarely use them. If you and your team use them quite often, then this is the way to go.

+4
source share

I suggest using XPath. Xalan is already included in the JDK (no external jars are required), and it meets your requirement, i.e. Iterate through the nodes of the elements (I suppose) and store 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 prints the corresponding element name and text content. Customize the xpath expression to suit your needs.

+4
source share

Try Apache Xerces . He is mature and reliable. Any such available alternatives will also take place, just make sure you don't deploy your own implementation.

+2
source share

Going around the whole question of parsing xml and storing values ​​in a database, I would like to ask a question about the need to do this. Most databases can now process xml, so you can somehow save them in a table without the need for parsing the contents; and the contents of such xml inside a column in a table can usually be queried using "xmlselect ()" and similar functions.

Think about it for a second; if in the near or distant future the xml content that you receive from another application changes, you will have many changes. If it changes frequently, it will become a nightmare.

Cheers, Wim

+2
source share

Try XStream , it is very simple.

0
source share

Well, I used stax to parse a fairly large number of XML nodes, which consumes less memory than Dom and sax, cauz is a style for pulling XML data. Stax may be a good choice for large XML data nodes.

0
source share

All Articles