What Java XML library do you recommend (to replace dom4j)? [closed]
Asked Answered
N

11

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

Nathanielnathanil answered 6/5, 2009 at 21:29 Comment(7)
It seems they've lost the dom4j.org domain (perhaps another sign that one should indeed replace it with something...) – the website is still available at dom4j.sourceforge.net though.Nathanielnathanil
Check few more posts on the topic: https://mcmap.net/q/76085/-best-xml-parser-for-java-closed and #26114. Also nice note about jdom/dom4j/xom is here: lists.xml.org/archives/xml-dev/200410/msg00492.htmlZayin
Ooh, just noticed there's finally a dom4j 2.0 alpha release out: sourceforge.net/projects/dom4j/files/dom4j-2.0.0-ALPHA-2 (They still haven't recovered their dom4j.org domain though.) Anyone tried it out?Nathanielnathanil
@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 questionNathanielnathanil
Another option is you can always help the dom4j team make a better dom4j. Grab a fork and dive in and submit some patches.Competence
even dom4j.sourceforge.net seems gone.Eightieth
Now dom4j.sourceforge.net redirects to dom4j.github.io, which does work (and is extremely simplistic).Nathanielnathanil
S
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.

Sessions answered 7/5, 2009 at 7:1 Comment(9)
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.Nathanielnathanil
Okay, I've been testing XOM (making use of Stack Overflow to provide nice little problems to tackle: #884487, #428573, #139576 etc. :-), and I'm starting to get conviced that I can do pretty much everything with XOM that I can do with dom4j!Nathanielnathanil
(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? #967788Nathanielnathanil
@Peter: I like XOM as well. Do you happen to know any performance comparisons, like sosnoski.com/opensrc/xmlbench/results.html and 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?Zayin
@dma_k: I am not aware of such comparison. Try asking at XOM mailing list at lists.ibiblio.org/mailman/listinfo/xom-interestShrieval
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.Reid
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 lists.ibiblio.org/pipermail/xom-interest/2010-March/004076.html), better StAX / TrAX API support), which dom4j has.Zayin
Does XOM provide a way to add child element at X positions of it's sibblings?Unclothe
@Cybrix: you mean single element at multiple positions? I don't think so.Shrieval
R
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.

Rheinland answered 7/5, 2009 at 12:11 Comment(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.Carreno
JAXB also is builtin and is pretty nice.Competence
S
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());
}
Subgroup answered 7/12, 2009 at 15:55 Comment(1)
This looks amazing, exactly what I need to support some data-driven testing.Revolutionist
F
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.

Farseeing answered 6/5, 2009 at 21:31 Comment(9)
Cool. Did you read the last sentence of my question? ;-)Nathanielnathanil
+1 - Simpler than all that W3C stuff.Saurel
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.Basaltware
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.Nathanielnathanil
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.Kansas
@Esko, just curious, have you tried XOM? Do you think it also sucks?Nathanielnathanil
If I choose JDOM vs. XOM, I choose XOM.Zayin
@cdmckay: Developement has been started again: We have 1.1.2Jovia
Looks like JDOM 2.0.0 was just released: "2.0.0 brings JDOM in to the world of Generics and other Java language items introduced with Java 5."Nathanielnathanil
K
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.

Kubetz answered 6/5, 2009 at 21:56 Comment(3)
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?Marissamarist
@Marissamarist no. You should use JAXB for something like that since it understands schema and WSDL.Competence
Not really an alternative for a general purpose XML API like dom4j. It's just a serializer/deserializer, isn't it?Haas
C
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).

Competence answered 2/4, 2011 at 13:44 Comment(1)
I wonder how JAXB 2.0 compares to a lightweight annotation-based XML lib like Simple, 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.)Nathanielnathanil
N
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.)

Nathanielnathanil answered 11/6, 2013 at 10:32 Comment(0)
M
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.

Marrakech answered 6/5, 2009 at 21:42 Comment(1)
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.Genesa
A
0

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

Ainslee answered 22/1, 2012 at 0:43 Comment(0)
H
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?

Hospodar answered 24/2, 2012 at 11:47 Comment(0)
D
0

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

Darrow answered 12/7, 2012 at 6:14 Comment(1)
What are the strengths of this tool?Cloister

© 2022 - 2024 — McMap. All rights reserved.