Alex Rivera | Logout

What Java XML library do you recommend (to replace dom4j)?

Asked 2009-05-06T21:29:54.667
72

I'm looking for something like dom4j, but without dom4j's warts, such as bad or missing documentation and seemingly stalled development status.

Background: I've been using and advocating dom4j, but don't feel completely right about it because I know the library is far from optimal (example: see how methods in XSLT related Stylesheet class are documented; what would you pass to run() as the String mode parameter?)

Requirements: The library should make basic XML handling easier than it is when using pure JDK (javax.xml and org.w3c.dom packages). Things like this:

  • Read an XML document (from file or String) into an object, easily traverse and manipulate the DOM, do XPath queries and run XSLT against it.
  • Build an XML document in your Java code, add elements and attributes and data, and finally write the document into a file or String.

I really like what dom4j promises, actually: "easy to use, open source library for working with XML, XPath and XSLT [...] with full support for DOM, SAX and JAXP." And upcoming

Edit
Report

3 Answers

3

In a recent project I had to do some XML parsing, and ended up using Simple Framework, recommended by a colleague.

I was quite happy with it in the end. It uses an annotation-based approach of mapping XML elements and attributes to Java classes and fields.

<example>
  <a>
    <b>
      <x>foo</x>
    </b>
    <b>
      <y>bar</y>
    </b>
  </a>
</example>

Corresponding Java code:

@Root
public class Example {

   @Path("a/b[1]")
   @Element
   private String x;

   @Path("a/b[2]")
   @Element
   private String y;
}

It's all quite different from dom4j or XOM. You avoid writing silly, boilerplatey XML handling code, but at first you'll probably bang your head against a wall for a while trying to get the annotations right.

(It was me who asked this question 4 years ago. While XOM seems a decent and quite popular dom4j replacement, I haven't come to fully embrace it. Curious that no-one had mentioned Simple Framework here. I decided to fix that, as I'd probably use it again.)

answered 2013-06-11T10:32:13.003
1

In our project we are using http://www.castor.org/ but just for small XML files. It's really easy to learn, needs just a mapping XML file (or none if the XML tags match perfectly class attributes) and it's done. It supports listeners (like callbacks) to perform additional processing. The cons: it is not a Java EE standard like JAXB.

answered 2009-05-06T21:42:21.313
0

For building XML documetns, I suggest xmlenc. It is used in cassandra.

answered 2012-07-12T06:14:03.467

Your Answer