What Java XML library do you recommend (to replace

2019-01-02 17:09发布

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

标签: java xml dom4j
11条回答
大哥的爱人
2楼-- · 2019-01-02 17:43

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.

查看更多
栀子花@的思念
3楼-- · 2019-01-02 17:44

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?

查看更多
谁念西风独自凉
4楼-- · 2019-01-02 17:45

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

查看更多
残风、尘缘若梦
5楼-- · 2019-01-02 17:46

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());
}
查看更多
大哥的爱人
6楼-- · 2019-01-02 17:54

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.

查看更多
残风、尘缘若梦
7楼-- · 2019-01-02 17:54

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.

查看更多
登录 后发表回答