views:

3447

answers:

6

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. :-)

+2  A: 

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 nagivation of node trees.

Steve B.
Cool. Did you read the last sentence of my question? ;-)
Jonik
+1 - Simpler than all that W3C stuff.
duffymo
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
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
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
@Esko, just curious, have you tried XOM? Do you think it also sucks?
Jonik
If I choose JDOM vs. XOM, I choose XOM.
dma_k
+1  A: 

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 JEE standard like JAXB.

Lluis Martinez
Castor does support JAXB however. And you have a choice of using a mapping file or of using an XML schema and Castor's code generator.
Eddie
I didn't know,thanks for the info!
Lluis Martinez
+3  A: 

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.

01
+9  A: 

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ý
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
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
(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
@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
@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ý
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
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
+2  A: 

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.

kdgregory
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
+1  A: 

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());
}
golimpio