Java DOM、SAX 和 StAX XML 解析器之间有什么区别?

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

What are the differences between DOM, SAX and StAX XML parsers?

javaxmldomsaxstax

提问by user3067088

I'm developing a RSS feed aggregator with Apache Tomcat. I was wondering which parser to use in order to read RSS feeds. Should I use DOM, SAX or StAX? I know that there are libraries specific to read RSS feeds with java but since this is a university project I am not supposed to use those. Thank you.

我正在使用 Apache Tomcat 开发 RSS 提要聚合器。我想知道使用哪个解析器来读取 RSS 提要。我应该使用 DOM、SAX 还是 StAX?我知道有专门用于使用 java 读取 RSS 提要的库,但由于这是一个大学项目,我不应该使用这些库。谢谢你。

回答by OldCurmudgeon

It mostly depends on your needs. Each has it's own features.

这主要取决于您的需求。每个都有自己的特点。

DOM- pull the whole thing into memory and walk around inside it. Good for comparatively small chunks of XML that you want to do complex stuff with. XSLT uses DOM.

DOM- 将整个事物拉入内存并在其中四处走动。适用于要处理复杂内容的相对较小的 XML 块。XSLT 使用 DOM。

SAX- Walk the XML as it arrives watching for things as they fly past. Good for large amounts of data or comparatively simple processing.

SAX- 在 XML 到达时遍历它,观察它们飞过的东西。适用于大量数据或相对简单的处理。

StAX- Much like SAX but instead of responding to events found in the stream you iterate through the xml - See When should I choose SAX over StAX?for discussion of which is best.

StAX- 很像 SAX,但不是响应在流中发现的事件,而是遍历 xml - 请参阅何时应该选择 SAX 而不是 StAX?讨论哪个最好。

There's a good discussion here Parsing XML using DOM, SAX and StAX Parser in Java- By Mohamed Sanaulla. NB: There's a fault in his SAX parser - he should appendcharacters, not replace them as character data is cumulative and may arrive in chunks.

这里有一个很好的讨论,在 Java 中使用 DOM、SAX 和 StAX 解析器解析 XML- 作者:Mohamed Sanaulla。注意:他的 SAX 解析器有问题 - 他应该附加字符,而不是替换它们,因为字符数据是累积的并且可能成块到达。

  content = String.copyValueOf(ch, start, length);

should be

应该

  content += String.copyValueOf(ch, start, length);

Also a blog post by Kaan YamanyarDifferences between DOM, SAX or StAX.

也是Kaan Yamanyar的博客文章DOM、SAX 或 StAX 之间的差异

回答by treeno

I don't know StAX, but I can say something to DOM and SAX:

我不知道 StAX,但我可以对 DOM 和 SAX 说点什么:

Dom holds the XML-Data in memory as a Object-Model. The advantage is, that you can access and change the data in a convenient and fast way in Memory. The disadvantage is, that this has a high memory consumption.

Dom 将 XML 数据作为对象模型保存在内存中。优点是,您可以方便快捷地访问和更改内存中的数据。缺点是,这具有很高的内存消耗。

SAX uses some kind of an event-pattern to read the data and doesn't keep any data in memory. The advantage is that this is relatively fast and doesn't need much memoryspace. The disadvantage is, that you have to create your own data-model if you want to change the data in a convenient way.

SAX 使用某种事件模式来读取数据,并且不会在内存中保留任何数据。优点是这样比较快,不需要太多内存空间。缺点是,如果您想以方便的方式更改数据,则必须创建自己的数据模型。

Dom is a little more complex to use compared to SAX.

与 SAX 相比,Dom 的使用稍微复杂一些。

Use SAX if you need to parse big data as a Stream. Use DOM if you want to hold the complete data in memory to work with it and the data-size is small enough to safely fit into memory.

如果您需要将大数据解析为流,请使用 SAX。如果您想将完整数据保存在内存中以使用它并且数据大小足够小以安全地放入内存,请使用 DOM。

For example: XSLT doesn't work with SAX because it needs to look forward in the data-stream while reading it. So it uses DOM even if that leads to memory-issues with big data.

例如:XSLT 不适用于 SAX,因为它需要在读取数据流时向前看。所以它使用 DOM,即使这会导致大数据的内存问题。

Hope that helped :-)

希望有所帮助:-)