java - StAX Cursor API 和 Iterator API 有什么区别?

标签 java xml stax

我遇到了使用 StAX 解析文件的两种不同代码。快速谷歌搜索告诉我有 2 种解析方法:使用 cursor API 和使用 iterator API。请告诉我两者之间的区别以及从开发人员的角度来看哪个更容易使用。

最佳答案

您最有可能在 Java Web 服务开发包教程或 Java EE 5 tutorial 中的 StAX API 部分找到答案。 .它们都包含我在下面复制的相同信息:

Comparing Cursor and Iterator APIs

Before choosing between the cursor and iterator APIs, you should note a few things that you can do with the iterator API that you cannot do with cursor API:

  • Objects created from the XMLEvent subclasses are immutable, and can be used in arrays, lists, and maps, and can be passed through your applications even after the parser has moved on to subsequent events.

  • You can create subtypes of XMLEvent that are either completely new information items or extensions of existing items but with additional methods.

  • You can add and remove events from an XML event stream in much simpler ways than with the cursor API.

Similarly, keep some general recommendations in mind when making your choice:

  • If you are programming for a particularly memory-constrained environment, like J2ME, you can make smaller, more efficient code with the cursor API.

  • If performance is your highest priority--for example, when creating low-level libraries or infrastructure--the cursor API is more efficient.

  • If you want to create XML processing pipelines, use the iterator API.

  • If you want to modify the event stream, use the iterator API.

  • If you want to your application to be able to handle pluggable processing of the event stream, use the iterator API.

  • In general, if you do not have a strong preference one way or the other, using the iterator API is recommended because it is more flexible and extensible, thereby "future-proofing" your applications.

尽管推荐使用迭代器 API,但它比游标 API 慢,因为游标不需要记住它已解析的先前节点;它提供 XML 文档的前向解析,并且没有构造 XMLEvent 对象的开销。显然,a benchmark表明与 StAX 相比,SAX 可能更擅长解析大型文档;您可能想要验证是否可以为您的数据集重现基准测试的结果。

关于java - StAX Cursor API 和 Iterator API 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6475754/

相关文章:

java - 如何更新 MongoDB 中的嵌套文档而不是使用 Spring Data Java

java - OpenGL函数拒绝渲染三角形

xml - 使用 XSLT 样式表的奇怪 XML 格式

Java解析大型XML文档

java - Header 上类型的身份验证具有未定义的属性 {http ://docs. oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Id

Java servlet Filter 在 FilterConfig 中具有多个值?是否可以?

c# - 将 XML 元素与 ListBox 所选项目匹配 - C#

java - XMLEventWriter : how can I tell it to write empty elements?

android - 如何在 Android 上满足 Apache POI 的依赖性?

java - 我的应用程序中要使用什么 Java XML API - StAX 还是 DOM?