views:

4730

answers:

6

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?

+2  A: 

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

bmatthews68
+12  A: 

I think you should not consider any specific parser implementation. Java API for XML Processing lets you use any conforming parser implementation in a standard way. The code should be much more portable, and when you realise that a specific parser has grown too old, you can replace it with another without changing a line of your code (if you do it correctly).

Basically there are two ways of handling XML in a standard way:

  • SAX This is the simplest API. You read/modify the XML by defining a Handler class that receives the data inside elements/attributtes when the XML gets processed in a serial way. It is faster and simpler if you only plan to read some attributes/elements and/or write some values back (your case).
  • DOM This method creates an object tree which lets you modify/access it randomly so it is better for complex XML manipulation and handling.

Forget about propietary APIs such as Jdom or Apache ones (i.e. Apache Xerces XMLSerializer) because will tie you to an specific implementation that can evolve in time or lose backwards compatibility, which will make you change your code in the future when you want to upgrade to a new version of Jdom or whatever parser you use. If you stick to Java standard API (using factories and interfaces) your code will be much more modular and maintenable.

There is no need to say that all (I haven't checked all, but I'm almost sure) of the parsers proposed comply with a JAXP implememtation so technically you can use all, no matter which.

Fernando Miguélez
Actually, 3 ways: StAX (javax.xml.stream) is the third standard one.
StaxMan
+3  A: 

If speed an memory is no problem dom4j is really good. 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.

zehrer
dom4j is pretty good, but definitely not without problems. For good dom4j alternatives, see http://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j
Jonik
+1  A: 

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

A: 

If you care less about performance, I'm a big fan of Apache Digester, since it essentially lets you map directly from XML to Java Beans.

Otherwise, you have to first parse, and then construct your objects.

Uri
I don't need to make Java Beans, just manipulate the raw XML elements a little, and review certain elements to get data from them, so a DOM style parser is probably my ideal solution.
Evan
Yea, dom4j would probably be a better solution there... I used to use it heavily, until I went one level up to digester
Uri
A: 

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.

Better, possible: faster, very unlikely.
StaxMan