Java 的最佳 XML 解析器

2022-01-30 00:00:00 xml parsing java

我需要读取较小的(最多几 MB,UTF-8 编码的)XML 文件,四处寻找各种元素和属性,也许修改一些并将 XML 再次写入磁盘(最好使用 nice,缩进格式).

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

什么是最适合我需要的 XML 解析器?有很多可供选择.我知道的一些是:

What would be the best XML parser for my needs? There are lots to choose from. Some I'm aware of are:

  • JDOM
  • 伍德斯托克斯
  • XOM
  • dom4j
  • VTD-XML
  • Xerces-J
  • 深红色

当然还有 JDK 中的那个(我使用的是 Java 6).我对 Xerces 很熟悉,但觉得它很笨重.

And of course the one in the JDK (I'm using Java 6). I'm familiar with Xerces but find it clunky.

建议?

推荐答案

如果速度和内存都没有问题,dom4j 是一个非常好的选择.如果您需要速度,使用像 Woodstox 这样的 StAX 解析器是正确的方法,但是您必须编写更多代码才能完成工作,并且您必须习惯于在流中处理 XML.

If speed and memory is no problem, dom4j is a really good option. 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.

相关文章