您推荐什么 Java XML 库(替换 dom4j)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/831865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:59:19  来源:igfitidea点击:

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

javaxmldom4j

提问by Jonik

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

我正在寻找类似 dom4j 的东西,但没有 dom4j 的缺点,例如糟糕的或丢失的文档以及看似停滞的开发状态。

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

背景:我一直在使用提倡dom4j,但感觉并不完全正确,因为我知道该库远非最佳(例如:查看 XSLT 相关样式表类中的方法是如何记录的;你会传递什么给 run( ) 作为String mode参数?)

Requirements: The library should make basic XML handling easier than it is when usingpure JDK(javax.xmland org.w3c.dompackages). Things like this:

要求:该库应该使基本的 XML 处理比使用纯 JDKjavax.xmlorg.w3c.dom包)更容易。像这样的事情:

  • 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.
  • 将 XML 文档(从文件或字符串)读入对象,轻松遍历和操作 DOM,执行 XPath 查询并针对它运行 XSLT。
  • 在 Java 代码中构建 XML 文档,添加元素、属性和数据,最后将文档写入文件或字符串。

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:

我真的很喜欢dom4j 的承诺,实际上:“易于使用的开源库,用于处理 XML、XPath 和 XSLT [...] 完全支持 DOM、SAX 和 JAXP。”即将推出的 dom4j 2.0 确实声称可以解决所有问题:充分利用 Java 5 并添加缺少的文档。但不幸的是,如果你仔细观察

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.

警告:dom4j 2.0 处于 pre-alpha 阶段。它可能无法编译。如果它可以随机编译,它很可能无法运行。如果它偶尔运行,它可能会突然爆炸。如果要使用 dom4j,则需要 1.6.1 版。真的。

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

...网站已经说了长时间了。那么 dom4j 有没有好的替代品呢?请为您喜欢的库提供一些理由,而不仅仅是转储名称和链接。:-)

回答by Steve B.

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

我一直很喜欢jdom。它被编写成比 DOM 解析更直观(无论如何,SAX 解析总是显得笨拙)。

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.

没有令人信服的理由让 Java API 操作 XML 变得复杂、棘手、不直观或令人头疼。JDOMTM 既以 Java 为中心,又以 Java 优化。它的行为类似于 Java,它使用 Java 集合,对于当前的 Java 开发人员来说,它是完全自然的 API,并且它为使用 XML 提供了一个低成本的入口点。

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

这几乎是我的经验 - 节点树的相当直观的导航。

回答by Lluis Martinez

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.

在我们的项目中,我们使用http://www.castor.org/但仅用于小型 XML 文件。它真的很容易学习,只需要一个映射 XML 文件(如果 XML 标签与类属性完全匹配,则不需要),就完成了。它支持侦听器(如回调)来执行额外的处理。缺点:它不像 JAXB 那样是 Java EE 标准。

回答by IAdapter

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

我使用XStream,它是一个简单的库,用于将对象序列化为 XML 并再次返回。

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

它可以是注解驱动的(如 JAXB),但它具有非常简单易用的 api,您甚至可以生成 JSON。

回答by Peter ?tibrany

Sure, XOM:-)

当然,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.

XOM 旨在易于学习和使用。它的工作非常直接,并且具有非常浅的学习曲线。假设您已经熟悉 XML,您应该能够非常快速地启动和运行 XOM。

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 已经好几年了,我仍然非常喜欢它。易于使用,网络上有大量文档和文章,API 在不同版本之间不会改变。1.2 是最近发布的。

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.

XOM 是唯一不影响正确性的 XML API。XOM 只接受命名空间格式良好的 XML 文档,并且只允许您创建命名空间格式良好的 XML 文档。(实际上,它比这更严格一些:它实际上保证所有文档都是可往返的并且具有定义良好的 XML 信息集。)XOM 管理您的 XML,因此您不必这样做。使用 XOM,您可以专注于应用程序的独特价值,并相信 XOM 可以正确地获得 XML。

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

查看网页http://www.xom.nu/以获取常见问题解答、食谱、设计原理等。如果一切都以如此多的爱设计就好了 :-)

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

作者还撰写了关于XML API 的问题(以及如何修复它们)。(基本上,XOM首先存在的原因)

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 JDOMand finally Design Principles and XOM.

这里也是5部分Artima采访约XOM,这里只讲作者有什么不对的XML API好的,坏的,和DOMJDOM的设计教训JDOM教训,最终设计原则和XOM

回答by kdgregory

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

JDK 中内置的那个……还有一些补充。

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

是的,使用起来很痛苦:它是根据委员会明确设计的 W3C 规范建模的。但是,它可以在任何地方使用,如果您决定使用它,您就不会遇到来自第三方库的“我喜欢 Dom4J”、“我喜欢 JDOM”、“我喜欢 StringBuffer”等参数。特别是因为这些参数可以使用不同的库变成不同的代码段......

However, as I said, I do enhance slightly: the Practical XMLlibrary 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.

但是,正如我所说,我确实做了一些改进:实用 XML库是一个实用程序类的集合,可以更轻松地使用 DOM。除了 XPath 包装器之外,这里没有什么复杂的东西,只有一堆我发现自己为每项工作重写的例程。

回答by Gilberto Olimpio

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

我一直在使用XMLTool来替换 Dom4j,它运行得很好。

XML Tool uses Fluent Interface pattern to facilitate XML manipulations:

XML Tool 使用 Fluent Interface 模式来促进 XML 操作:

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:

它是为 Java 5 设计的,很容易在所选元素上创建一个可迭代对象:

for (XMLTag xmlTag : tag.getChilds()) {
   System.out.println(xmlTag.getCurrentTagName());
}

回答by Adam Gent

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

我将在@kdgregory 的内置答案中补充说为什么不使用 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?

使用一些注释可以很容易地为大多数 XML 文档建模。我的意思是你可能会解析这些东西并放入一个对象,对吗?

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

JAXB 2.0 内置于 JDK 1.6 中,与许多其他内置 javax 库不同,这个库非常好(Kohusuke 致力于它,所以你知道它的好)。

回答by Taran Singh

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

您可以尝试使用 JAXB,它的注释非常方便和简单:用于 XML 绑定的 Java 架构。

回答by Danubian Sailor

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

我有时会使用Jericho,它主要是 HTML 解析器,但可以解析任何类似 XML 的结构。

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?

当然,它只是针对最简单的 XML 操作,例如查找具有给定名称的标签、遍历结构、替换标签及其属性,但这不是最多的用例吗?

回答by Jingguo Yao

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

对于构建 XML 文档,我建议使用xmlenc。它用于cassandra