Java:解析XML文件:DOM,SAX或StAX? [英] Java: Parsing XML files: DOM, SAX or StAX?

查看:162
本文介绍了Java:解析XML文件:DOM,SAX或StAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Tomcat开发一个RSS feed聚合器。我想知道为了阅读RSS提要而使用哪个解析器。我应该使用DOM,SAX还是StAX?我知道有特定的图书馆读取RSS feed与java,但由于这是一个大学项目,我不应该使用那些。
谢谢。

解决方案

这主要取决于你的需要。每个都有它自己的功能。



DOM - 将整个内容拉入内存并在其中四处走动。对于您想要做复杂的XML的相对较小的XML块很有用。 XSLT使用DOM。



SAX - 随着XML越来越流行,随时随地观看事物。对于大量的数据或比较简单的处理,可以使用。



StAX - 很像SAX,而不是响应流中发现的事件,您可以迭代通过xml - 请参阅什么时候应该选择SAX over StAX?,以便讨论哪个是最好的。



这里有一个很好的讨论使用DOM中的DOM,SAX和StAX解析器解析XML - 由Mohamed Sanaulla发表。注意:他的SAX解析器有一个错误 - 他应该追加字符,而不是替换它们,因为字符数据是累积的,可能会以块的形式出现。

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

应该是

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

另外还有一个由Kaan Yamanyar发表的博客 DOM,SAX或StAX之间的差异


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.

解决方案

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.

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

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.

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 append characters, not replace them as character data is cumulative and may arrive in chunks.

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

should be

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

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

这篇关于Java:解析XML文件:DOM,SAX或StAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆