适用于Java的最佳XML解析器[关闭]

本文翻译自:Best XML parser for Java [closed]

I need to read smallish (few MB at the most, UTF-8 encoded) XML files, rummage around looking at various elements and attributes, perhaps modify a few and write the XML back out again to disk (preferably with nice, indented formatting). 我需要阅读较小的(最多几个MB,UTF-8编码的)XML文件,浏览各种元素和属性,或许修改一些并将XML再次写回磁盘(最好使用漂亮的缩进格式) 。

What would be the best XML parser for my needs? 什么是最符合我需求的XML解析器? There are lots to choose from. 有很多可供选择。 Some I'm aware of are: 我知道的有些是:

And of course the one in the JDK (I'm using Java 6). 当然是JDK中的那个(我使用的是Java 6)。 I'm familiar with Xerces but find it clunky. 我对Xerces很熟悉,但发现它很笨重。

Recommendations? 建议?


#1楼

参考:https://stackoom.com/question/1ZFZ/适用于Java的最佳XML解析器-关闭


#2楼

I have found dom4j to be the tool for working with XML. 我发现dom4j是使用XML的工具。 Especially compared to Xerces. 特别是与Xerces相比。


#3楼

I think you should not consider any specific parser implementation. 我认为你不应该考虑任何特定的解析器实现。 Java API for XML Processing lets you use any conforming parser implementation in a standard way. Java API for XML Processing允许您以标准方式使用任何符合标准的解析器实现。 The code should be much more portable, and when you realise that a specific parser has grown too old, you can replace it with another without changing a line of your code (if you do it correctly). 代码应该更加可移植,并且当您意识到特定解析器已经变得太旧时,您可以在不更改代码行的情况下将其替换为另一行(如果您正确执行)。

Basically there are three ways of handling XML in a standard way: 基本上有三种以标准方式处理XML的方法:

  • SAX This is the simplest API. SAX这是最简单的API。 You read the XML by defining a Handler class that receives the data inside elements/attributes when the XML gets processed in a serial way. 您通过定义Handler类来读取XML,该类在以串行方式处理XML时接收元素/属性内的数据。 It is faster and simpler if you only plan to read some attributes/elements and/or write some values back (your case). 如果您只打算读取一些属性/元素和/或写回一些值(您的情况),它会更快更简单。
  • DOM This method creates an object tree which lets you modify/access it randomly so it is better for complex XML manipulation and handling. DOM此方法创建一个对象树,允许您随机修改/访问它,以便更好地进行复杂的XML操作和处理。
  • StAX This is in the middle of the path between SAX and DOM. StAX这是SAX和DOM之间的路径中间。 You just write code to pull the data from the parser you are interested in when it is processed. 您只需编写代码即可在处理数据时从您感兴趣的解析器中提取数据。

Forget about proprietary APIs such as JDOM or Apache ones (ie Apache Xerces XMLSerializer ) because will tie you to a specific implementation that can evolve in time or lose backwards compatibility, which will make you change your code in the future when you want to upgrade to a new version of JDOM or whatever parser you use. 忘记专有API,例如JDOM或Apache专用API(即Apache Xerces XMLSerializer ),因为它会将您绑定到可能及时发展或失去向后兼容性的特定实现,这将使您在将来要升级时更改代码新版本的JDOM或您使用的任何解析器。 If you stick to Java standard API (using factories and interfaces) your code will be much more modular and maintainable. 如果您坚持使用Java标准API(使用工厂和接口),您的代码将更加模块化和可维护。

There is no need to say that all (I haven't checked all, but I'm almost sure) of the parsers proposed comply with a JAXP implementation so technically you can use all, no matter which. 没有必要说所有(我没有检查所有,但我几乎可以肯定)解析器建议符合JAXP实现,所以从技术上讲,你可以使用所有,无论哪个。


#4楼

If speed and memory is no problem, dom4j is a really good option. 如果速度和内存没问题, dom4j是一个非常好的选择。 If you need speed, using a StAX parser like Woodstox is the right way, but you have to write more code to get things done and you have to get used to process XML in streams. 如果你需要速度,使用像Woodstox这样的StAX解析器是正确的方法,但你必须编写更多的代码来完成工作,你必须习惯于在流中处理XML。


#5楼

除了SAX和DOM之外,还可以使用XMLStreamReader进行STaX解析,XMLStreamReader是一个xml pull解析器。


#6楼

If you care less about performance, I'm a big fan of Apache Digester, since it essentially lets you map directly from XML to Java Beans. 如果你不太关心性能,我是Apache Digester的忠实粉丝,因为它本质上允许你直接从XML映射到Java Bean。

Otherwise, you have to first parse, and then construct your objects. 否则,您必须首先解析,然后构造您的对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值