74

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 dom4j 2.0 does claim to fix everything: fully utilise Java 5 and add missing documentation. But unfortunately, if you look closer:

Warning: dom4j 2.0 is in pre-alpha stage. It is likely it can't be compiled. In case it can be compiled at random it is likely it can't run. In case it runs occasionally it can explode suddenly. If you want to use dom4j, you want version 1.6.1. Really.

...and the website has said that for a long time. So is there a good alternative to dom4j? Please provide some justification for your preferred library, instead of just dumping names and links. :-)

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • 7
    It seems they've lost the http://dom4j.org domain (perhaps another sign that one should indeed replace it with something...) – the website is still available at http://dom4j.sourceforge.net though. – Jonik Dec 28 '09 at 23:30
  • 2
    Check few more posts on the topic: http://stackoverflow.com/questions/373833/best-xml-parser-for-java and http://stackoverflow.com/questions/25614/which-java-dom-wrapper-is-the-best-or-most-popular. Also nice note about jdom/dom4j/xom is here: http://lists.xml.org/archives/xml-dev/200410/msg00492.html – dma_k Mar 18 '10 at 13:12
  • Ooh, just noticed there's finally a dom4j 2.0 alpha release out: https://sourceforge.net/projects/dom4j/files/dom4j-2.0.0-ALPHA-2/ (They still haven't recovered their http://dom4j.org domain though.) Anyone tried it out? – Jonik May 03 '10 at 14:23
  • @vtd-xml. No, I hadn't heard of it... Consider posting a full answer about it if you think it's a good dom4j replacement as laid out in the question – Jonik Aug 09 '10 at 08:57
  • Another option is you can always help the dom4j team make a better dom4j. Grab a fork and dive in and submit some patches. – Adam Gent Apr 02 '11 at 14:26
  • even http://dom4j.sourceforge.net/ seems gone. – Bhabani Sankar Mishra Jul 20 '15 at 04:53
  • Now http://dom4j.sourceforge.net/ redirects to http://dom4j.github.io/, which does work (and is extremely simplistic). – Jonik Jul 26 '15 at 19:40

11 Answers11

53

Sure, XOM :-)

XOM is designed to be easy to learn and easy to use. It works very straight-forwardly, and has a very shallow learning curve. Assuming you're already familiar with XML, you should be able to get up and running with XOM very quickly.

I use XOM for several years now, and I still like it very much. Easy to use, plenty of documentation and articles on the web, API doesn't change between releases. 1.2 was released recently.

XOM is the only XML API that makes no compromises on correctness. XOM only accepts namespace well-formed XML documents, and only allows you to create namespace well-formed XML documents. (In fact, it's a little stricter than that: it actually guarantees that all documents are round-trippable and have well-defined XML infosets.) XOM manages your XML so you don't have to. With XOM, you can focus on the unique value of your application, and trust XOM to get the XML right.

Check out web page http://www.xom.nu/ for FAQ, Cookbook, design rationale, etc. If only everything was designed with so much love :-)

Author also wrote about What's Wrong with XML APIs (and how to fix them). (Basically, reasons why XOM exists in the first place)

Here is also 5-part Artima interview with author about XOM, where they talk about what's wrong with XML APIs, The Good, the Bad, and the DOM, A Design Review of JDOM, Lessons Learned from JDOM and finally Design Principles and XOM.

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
  • XOM does look promising, especially considering how its creator clearly knew the existing libraries very well, and tried improve on them. Compared to dom4j and JDOM, it also seems somewhat more active: latest release was in March 2009 (although the one before that was in Aug 2006). I'm trying out XOM one of these days to see if I can really use it with similar or greater ease as dom4j. – Jonik May 16 '09 at 13:17
  • Okay, I've been testing XOM (making use of Stack Overflow to provide nice little problems to tackle: http://stackoverflow.com/questions/883987/java-appending-xml-docs-to-existing-docs/966886#966886, http://stackoverflow.com/questions/428073/what-is-the-best-simplest-way-to-read-in-an-xml-file-in-java-application/963648#963648, http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java/962225#962225 etc. :-), and I'm starting to get conviced that I can do pretty much everything with XOM that I can do with dom4j! – Jonik Jun 08 '09 at 22:20
  • (cont) However, one thing I'm having doubts about is streaming XML data, which I've done with dom4j before. Here's a question I wrote about it, perhaps you know the answer? http://stackoverflow.com/questions/967288/how-to-stream-xml-data-using-xom – Jonik Jun 08 '09 at 22:22
  • @Peter: I like XOM as well. Do you happen to know any performance comparisons, like http://sosnoski.com/opensrc/xmlbench/results.html and http://www.xml.com/lpt/a/1703 that also include XOM in the list among others? Do you expect any big difference in comparison to jdom/dom4j? – dma_k Mar 18 '10 at 13:09
  • @dma_k: I am not aware of such comparison. Try asking at XOM mailing list at http://lists.ibiblio.org/mailman/listinfo/xom-interest – Peter Štibraný Mar 18 '10 at 18:15
  • 11
    Beware. Transforming dom4j/jdom to XOM is a pain in the ass. Furthermore, the API does not support Java collections. Both are very sore points. – Gili Apr 02 '10 at 18:55
  • 4
    Just to add my opinion after working with XOM a bit. Definitely it is better then JDOM. But it lacks certain sugar features, which I would like to see in XML API (`Element.setAttribute(name, value)`, `Element.setText(string)`, visitor pattern (see http://lists.ibiblio.org/pipermail/xom-interest/2010-March/004076.html), better StAX / TrAX API support), which dom4j has. – dma_k Sep 16 '10 at 17:45
  • Does XOM provide a way to add child element at X positions of it's sibblings? – Cybrix Oct 23 '11 at 14:38
  • @Cybrix: you mean single element at multiple positions? I don't think so. – Peter Štibraný Oct 23 '11 at 15:57
18

The one built into the JDK ... with a few additions.

Yes, it's painful to use: it is modeled after W3C specs that were clearly designed by committee. However, it is available anywhere, and if you settle on it you don't run into the "I like Dom4J," "I like JDOM," "I like StringBuffer" arguments that come from third-party libraries. Especially since such arguments can turn into different pieces of code using different libraries ...

However, as I said, I do enhance slightly: the Practical XML library is a collection of utility classes that make it easier to work with the DOM. Other than the XPath wrapper, there's nothing complex here, just a bunch of routines that I found myself rewriting for every job.

Flow
  • 23,572
  • 15
  • 99
  • 156
kdgregory
  • 38,754
  • 10
  • 77
  • 102
  • 2
    I agree. I used to swear by JDom, but found that once I wrote a few helper methods, the JAX-P stuff just isn't any more difficult and it's installed everywhere. – Dave Ray Dec 07 '09 at 16:10
  • 1
    JAXB also is builtin and is pretty nice. – Adam Gent Apr 02 '11 at 13:45
10

I've been using XMLTool for replacing Dom4j and it's working pretty well.

XML Tool uses Fluent Interface pattern to facilitate XML manipulations:

XMLTag tag = XMLDoc.newDocument(false)
   .addDefaultNamespace("http://www.w3.org/2002/06/xhtml2/")
   .addNamespace("wicket", "http://wicket.sourceforge.net/wicket-1.0")
   .addRoot("html")
   .addTag("wicket:border")
   .gotoRoot().addTag("head")
   .addNamespace("other", "http://other-ns.com")
   .gotoRoot().addTag("other:foo");
System.out.println(tag.toString());

It's made for Java 5 and it's easy to create an iterable object over selected elements:

for (XMLTag xmlTag : tag.getChilds()) {
   System.out.println(xmlTag.getCurrentTagName());
}
Gilberto Olimpio
  • 681
  • 14
  • 23
6

I've always liked jdom. It was written to be more intuitive than DOM parsing(and SAX parsing always seems clumsy anyway).

From the mission statement:

There is no compelling reason for a Java API to manipulate XML to be complex, tricky, unintuitive, or a pain in the neck. JDOMTM is both Java-centric and Java-optimized. It behaves like Java, it uses Java collections, it is completely natural API for current Java developers, and it provides a low-cost entry point for using XML.

That's pretty much been my experience - fairly intuitive navigation of node trees.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Steve B.
  • 55,454
  • 12
  • 93
  • 132
  • Cool. Did you read the last sentence of my question? ;-) – Jonik May 06 '09 at 21:35
  • +1 - Simpler than all that W3C stuff. – duffymo May 06 '09 at 21:35
  • 2
    I've used JDOM recently on a project, and while the API is intuitive, I have two problems with it: development seems to have stalled (last update was the end of 2007) and the library has not yet been generified. – cdmckay May 06 '09 at 21:48
  • Steve, thanks for elaborating. But if what cdmckay says is true, then JDOM suffers from (some of) the same problems as dom4j. Its API documentation does seem better though, judging from a quick look. – Jonik May 06 '09 at 22:34
  • My two recommendations: 1) None, for some reason they all suck. 2) ...wel OK, if you must then JDOM. Because of the latter I upvoted this. – Esko May 07 '09 at 07:04
  • @Esko, just curious, have you tried XOM? Do you think it also sucks? – Jonik Jun 08 '09 at 09:43
  • 1
    If I choose JDOM vs. XOM, I choose XOM. – dma_k Sep 16 '10 at 17:20
  • @cdmckay: Developement has been started again: We have 1.1.2 – Martin Schröder Feb 22 '12 at 14:16
  • 3
    Looks like [JDOM 2.0.0 was just released](http://www.jdom.org/news/index.html): "2.0.0 brings JDOM in to the world of Generics and other Java language items introduced with Java 5." – Jonik Apr 09 '12 at 15:22
5

I use XStream, its a simple library to serialize objects to XML and back again.

it can be annotation-driven (like JAXB), but it has very simple and easy to use api and you can even generate JSON.

Jonik
  • 80,077
  • 70
  • 264
  • 372
IAdapter
  • 62,595
  • 73
  • 179
  • 242
  • Seems very simple. Just wondering how it actually works: if I have an XML downloaded from the internet, how does XStream know which class should it be deserialized to? – ettore Mar 10 '11 at 09:23
  • @ettore no. You should use JAXB for something like that since it understands schema and WSDL. – Adam Gent Apr 02 '11 at 14:27
  • 1
    Not really an alternative for a general purpose XML API like dom4j. It's just a serializer/deserializer, isn't it? – zehrer Nov 15 '11 at 08:34
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.)

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
3

I'll add to the built-in answer by @kdgregory by saying why not JAXB?

With a few annotations its pretty easy to model most XML documents. I mean your probably going to parse the stuff and put in an object right?

JAXB 2.0 is built in to JDK 1.6 and unlike many other builtin javax libraries this one is pretty good (Kohusuke worked on it so you know its good).

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • I wonder how JAXB 2.0 compares to a lightweight annotation-based XML lib like [Simple](http://stackoverflow.com/a/17041986/56285), in terms of simplicity and avoiding boilerplate..? (Back in the day when I asked this question, I sort of missed JAXB altogether, but I guess deserves a closer look, if one needs to do XML with Java.) – Jonik Nov 08 '15 at 19:07
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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Lluis Martinez
  • 1,963
  • 8
  • 28
  • 42
0

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

Jingguo Yao
  • 7,320
  • 6
  • 50
  • 63
0

you can try JAXB, with annotations its very handy and simple to do: Java Architecture for XML Binding.

Michael Dillon
  • 1,037
  • 6
  • 16
Taran Singh
  • 583
  • 2
  • 6
  • 13
0

I'm sometimes using Jericho, which is primarily HTML parser, but can parse any XML-like structure.

Of course it is only for the simplest XML operations, such as finding tags with given name, iterating through structure, replacing tags and its attributes, but aren't this the most use cases?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Danubian Sailor
  • 1
  • 38
  • 145
  • 223