在哪里可以找到 Java XML 框架的详细比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3855324/
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-10-30 03:42:35  来源:igfitidea点击:

Where I can find a detailed comparison of Java XML frameworks?

javaxmlarchitecturejaxbxml-parsing

提问by yegor256

I'm trying to choose an XML-processing framework for my Java projects, and I'm lost in names.. XOM, JDOM, etc. Where I can find a detailed comparison of all popular Java XML frameworks?

我正在尝试为我的 Java 项目选择一个 XML 处理框架,但我在名称中迷失了……XOM、JDOM 等。在哪里可以找到所有流行的 Java XML 框架的详细比较?

回答by Aravind Yarram

As Blaise pointed out stick with the standards. But there are multiple standards created over the period to solve different problems/usecases. Which one to choose completely depends upon your requirement. I hope the below comparison can help you choose the right one.

正如布莱斯指出的那样,坚持标准。但是在此期间创建了多个标准来解决不同的问题/用例。选择哪一个完全取决于您的要求。我希望下面的比较可以帮助您选择合适的。

Now there are two thingsyou have to choose. APIand the implementations of the API(there are many)

现在有两件事你必须选择。APIAPI实现(有很多)

API

应用程序接口

SAX: Pros

SAX:优点

  • event based
  • memory efficient
  • faster than DOM
  • supports schema validation
  • 基于事件
  • 内存高效
  • 比 DOM 快
  • 支持模式验证

SAX: Cons

SAX:缺点

  • No object model, you have to tap into the events and create your self
  • Single parse of the xml and can only go forward
  • read only api
  • no xpath support
  • little bit harder to use
  • 没有对象模型,您必须利用事件并创建自己
  • xml的单一解析,只能前进
  • 只读api
  • 没有 xpath 支持
  • 有点难用

DOM: Pros

DOM:优点

  • in-memory object model
  • preserves element order
  • bi-directional
  • read and write api
  • xml MANIPULATION
  • simple to use
  • supports schema validation
  • 内存对象模型
  • 保留元素顺序
  • 双向
  • 读写api
  • xml 操作
  • 使用简单
  • 支持模式验证

DOM: Cons

DOM:缺点

  • memory hog for larger XML documents (typically used for XML documents less than 10 mb)
  • slower
  • generic model i.e. you work with Nodes
  • 较大 XML 文档的内存占用(通常用于小于 10 mb 的 XML 文档)
  • 慢点
  • 通用模型,即您使用节点

Stax: Pros

Stax:优点

  • Best of SAX and DOM i.e. Ease of DOM and efficiency of SAX
  • memory efficient
  • Pull model
  • read and write api
  • supports subparsing
  • can read multiple documents same time in one single thread
  • parallel processing of XML is easier
  • SAX 和 DOM 的精华,即 DOM 的易用性和 SAX 的效率
  • 内存高效
  • 拉模型
  • 读写api
  • 支持子解析
  • 可以在一个线程中同时阅读多个文档
  • XML 的并行处理更容易

Stax: Cons

税:缺点

  • no schema validation support (as far as I remember, not sure if they have added it now)
  • can only go forward like sax
  • no xml MANIPULATION
  • 没有模式验证支持(据我所知,不确定他们现在是否添加了它)
  • 只能像萨克斯一样前进
  • 没有 xml 操作

JAXB: Pros

JAXB:优点

  • allows you to access and process XML data without having to know XML
  • bi-directional
  • more memory efficient than DOM
  • SAX and DOM are generic parsers where as JAXB creates a parser specific to your XML Schmea
  • data conversion: JAXB can convert xml to java types
  • supports XML MANIPULATION via object API
  • 允许您访问和处理 XML 数据而无需了解 XML
  • 双向
  • 内存效率比 DOM 高
  • SAX 和 DOM 是通用解析器,而 JAXB 创建特定于您的 XML Schmea 的解析器
  • 数据转换:JAXB 可以将 xml 转换为 java 类型
  • 通过对象 API 支持 XML MANIPULATION

JAXB: Cons

JAXB:缺点

  • can only parse valid XML
  • 只能解析有效的 XML

Trax:For transforming XML from 1 form to another form using XSLT

Trax:用于使用 XSLT 将 XML 从一种形式转换为另一种形式

Implementations

实现

SAX, DOM, Stax, JAXB are just specifications. There are many open source and commercial implementations of these specifications. Most of the time you can just stick with what comes with JDK or your application server. But sometimes you need to use a different implementation that provided by default. And this is where you can appreciate the JAXP wrapper api. JAXPallows you to switch implementations through configuration without the need to modify your code. It also provides a parser/spec independent api for parsing, transformation, validation and querying XML documents.

SAX、DOM、Stax、JAXB 只是规范。这些规范有许多开源和商业实现。大多数情况下,您可以坚持使用 JDK 或您的应用程序服务器附带的内容。但有时您需要使用默认提供的不同实现。这是您可以欣赏JAXP 包装器 api 的地方JAXP允许您通过配置切换实现,而无需修改您的代码。它还提供了一个独立于解析器/规范的 API,用于解析、转换、验证和查询 XML 文档。

Performance and other comparisons of various implementations

各种实现的性能和其他比较



Now standards are good but once in a while you encounter this crazy usecase where you have to support parsing of XML document that is 100 gigabytesof size or you need ultra fastprocessing of XML (may be your are implementing a XML parser chip) and this is when you need to dump the standards and look for a different way of doing things. Its about using the right tool for the right job! And this is where I suggest you to have a look at vtd-xml

现在标准很好,但偶尔你会遇到这个疯狂的用例,你必须支持100 GB大小的 XML 文档的解析,或者你需要超快速处理 XML(可能是你正在实现一个 XML 解析器芯片)和这个当您需要抛弃标准并寻找不同的做事方式时。它是关于为正确的工作使用正确的工具!这就是我建议你看看vtd-xml 的地方

During the initial days of SAX and DOM, people wanted simpler API's than provided by either of them. JDOM, dom4j, XmlBeans, JiBX, Castorare the ones I know that became popular.

在 SAX 和 DOM 的最初日子里,人们想要比它们提供的 API 更简单的 API。JDOMdom4jXmlBeansJiBXCastor是我所知道的流行的那些。

回答by 14all41

@Pangea

@Pangea

JAXB vs DOM and SAX

JAXB 与 DOM 和 SAX

JAXB is not directly comparable to DOM and SAX. The Java DOM and SAX parsing APIs are lower-level APIs to parse XML documents, while JAXB (Java API for XML Binding) is a higher-level API for converting XML elements and attributes to a Java object hierarchy (and vice versa). Implementations of JAXB will most likely use a DOM or SAX parser behind the scenes to do the actual parsing of the XML input data.

JAXB 不能直接与 DOM 和 SAX 相提并论。Java DOM 和 SAX 解析 API 是用于解析 XML 文档的低级 API,而 JAXB(用于 XML 绑定的 Java API)是用于将 XML 元素和属性转换为 Java 对象层次结构(反之亦然)的高级 API。JAXB 的实现很可能在幕后使用 DOM 或 SAX 解析器来对 XML 输入数据进行实际解析。