Advertisement
Advertisement


Best XML parser for Java


Question

I need to read smallish (few MB at the most, UTF-8 encoded) XML files, rummage around looking at various elements and attributes, perhaps modify a few and write the XML back out again to disk (preferably with nice, indented formatting).

What would be the best XML parser for my needs? There are lots to choose from. Some I'm aware of are:

And of course the one in the JDK (I'm using Java 6). I'm familiar with Xerces but find it clunky.

Recommendations?

2015/10/26
1
388
10/26/2015 3:22:57 AM

Accepted Answer

If speed and memory is no problem, dom4j is a really good option. If you need speed, using a StAX parser like Woodstox is the right way, but you have to write more code to get things done and you have to get used to process XML in streams.

2013/12/24
82
12/24/2013 8:57:33 AM


Here is a nice comparision on DOM, SAX, StAX & TrAX (Source: http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/SJSXP2.html )

Feature                  StAX                  SAX                      DOM                  TrAX

API Type                Pull,streaming     Push,streaming    In memory tree    XSLT Rule

Ease of Use          High                    Medium                 High                    Medium

XPath Capability   No                       No                        Yes                      Yes

CPU & Memory     Good                  Good                    Varies                  Varies

Forward Only        Yes                    Yes                        No                       No

Read XML              Yes                    Yes                        Yes                     Yes

Write XML              Yes                    No                          Yes                     Yes

CRUD                      No                      No                         Yes                     No

2011/04/14

Simple XML http://simple.sourceforge.net/ is very easy for (de)serializing objects.

2011/07/23

In addition to SAX and DOM there is STaX parsing available using XMLStreamReader which is an xml pull parser.

2008/12/18

I have found dom4j to be the tool for working with XML. Especially compared to Xerces.

2013/12/24

I wouldn't recommended this is you've got a lot of "thinking" in your app, but using XSLT could be better (and potentially faster with XSLT-to-bytecode compilation) than Java manipulation.

2008/12/18

Source: https://stackoverflow.com/questions/373833
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]