您推荐什么 Java XML 库(替换 dom4j)? [英] What Java XML library do you recommend (to replace dom4j)?

查看:34
本文介绍了您推荐什么 Java XML 库(替换 dom4j)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

要求:该库应该使基本的 XML 处理比 使用 纯JDK(javax.xmlorg.w3c.dom 包).类似的事情:

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

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

<块引用>

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

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

解决方案

好的,XOM :-)<块引用>

XOM 旨在易于学习并且易于使用.它非常有效直截了当,并有一个非常学习曲线浅.假设你已经熟悉 XML,你应该能够启动并运行使用 XOM 非常快.

我使用 XOM 已经好几年了,我仍然非常喜欢它.易于使用,网络上有大量文档和文章,API 在不同版本之间不会改变.1.2 最近发布.

<块引用>

XOM 是唯一的 XML API在正确性上妥协.仅 XOM接受命名空间格式良好的 XML文件,并且只允许您创建命名空间格式良好的 XML文件.(其实有点比那更严格:它实际上保证所有文件都是可往返并具有明确定义XML 信息集.)XOM 管理您的 XML,因此你不必.使用 XOM,您可以专注于您的独特价值应用程序,并信任 XOM 来获取XML 正确.

查看网页 http://www.xom.nu/ 以获取常见问题解答、食谱、设计原理,等等.如果一切都是用这么多爱来设计的:-)

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

这也是 Artima 对作者关于 XOM 的 5 部分采访,他们谈论了有什么问题XML API好的、坏的和 DOMJDOM 的设计审查,从 JDOM 中吸取的教训 和最后 设计原理和 XOM.

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

解决方案

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.

这篇关于您推荐什么 Java XML 库(替换 dom4j)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆