问题描述
像SAX和StAX这样的流式xml解析器比构建像DOM解析器这样的树结构的解析器更快,内存效率更高。 SAX是一个推送解析器,意味着它是观察者模式的一个实例(也称为监听器模式)。 SAX首先出现了,但随后出现了StAX - 一个拉解析器,这意味着它基本上就像迭代器一样。
Streaming xml-parsers like SAX and StAX are faster and more memory efficient than parsers building a tree-structure like DOM-parsers. SAX is a push parser, meaning that it's an instance of the observer pattern (also called listener pattern). SAX was there first, but then came StAX - a pull parser, meaning that it basically works like an iterator.
你可以找到为什么更喜欢StAX而不是SAX的原因,但它通常归结为:它更容易使用。
You can find reasons why to prefer StAX over SAX everywhere, but it usually boils down to: "it's easier to use".
在JAXP的Java教程中,StAX被模糊地呈现为DOM和SAX之间的中间:它更容易比SAX更有效,而且比DOM更有效。但是,我从来没有找到任何线索,说明StAX比SAX更慢或内存效率更低。
In the Java tutorial on JAXP StAX is vaguely presented as the middle between DOM and SAX: "it's easier than SAX and more efficient than DOM". However, I never found any clues that StAX would be slower or less memory efficient than SAX.
这一切让我想知道:有没有理由选择SAX而不是StAX?
推荐答案
为了概括一点,我认为 StAX
可以与 SAX
一样高效。随着 StAX的改进设计
我无法真正找到 SAX
解析的任何情况除非使用遗留代码,否则将是首选。
To generalize a bit, I think StAX
can be as efficient as SAX
. With the improved design of StAX
I can't really find any situation where SAX
parsing would be preferred, unless working with legacy code.
编辑:根据此博客 StAX
不提供模式验证。
EDIT: According to this blog Java SAX vs. StAX StAX
offer no schema validation.
这篇关于什么时候应该选择SAX而不是StAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!