简体   繁体   English

Android:DOM vs SAX vs XMLPullParser解析?

[英]Android: DOM vs SAX vs XMLPullParser parsing?

I am parsing XML Document using SAX Parser. 我正在使用SAX Parser解析XML Document。

I want to know which is better and faster to work with DOM , SAX Parser or XMLPullParser . 我想知道使用DOMSAX ParserXMLPullParser哪个更好更快。

it depends on what are you doing , if you have very large files then you should use SAX parser since it will fire events and releasing them ,nothing is stored in memory ,and using SAX parser you can't access element in a random way there is no going back ! 这取决于你在做什么,如果你有非常大的文件,那么你应该使用SAX解析器,因为它会触发事件并释放它们,没有任何东西存储在内存中,并且使用SAX解析器你不能随机访问元素那里是不会回来的! , but Dom let you access any part of the xml file since it keeps the whole file/document in memory . 但是Dom允许您访问xml文件的任何部分,因为它将整个文件/文档保存在内存中。 hope this answer you question . 希望这个回答你的问题。

if you want to know which fastest parser Xerces is going to be the fastest you'll find and SAX parser should give you more performance than Dom 如果你想知道哪个最快的解析器Xerces将是你找到的最快的,SAX解析器应该给你比Dom更多的性能

One aspect by which different kinds of parsers can be classified is whether they need to load the entire XML document into memory up front. 可以对不同类型的解析器进行分类的一个方面是它们是否需要预先将整个XML文档加载到存储器中。 Parsers based on the Document Object Model (DOM) do that: they parse XML documents into a tree structure, which can then be traversed in-memory to read its contents. 基于文档对象模型(DOM)的解析器可以做到:它们将XML文档解析为树结构,然后可以在内存中遍历该结构以读取其内容。 This allows you to traverse a document in arbitrary order, and gives rise to some useful APIs that can be slapped on top of DOM, such as XPath, a path query language that has been specifically designed for extracting information from trees. 这允许您以任意顺序遍历文档,并产生一些可以在DOM之上打开的有用API,例如XPath,一种专门用于从树中提取信息的路径查询语言。 Using DOM alone isn't much of a benefit because its API is clunky and it's expensive to always read everything into memory even if you don't need to. 单独使用DOM并没有太大的好处,因为它的API很笨重,即使你不需要,也总是将所有东西都读入内存是很昂贵的。 Hence, DOM parsers are, in most cases, not the optimal choice to parse XML on Android. 因此,在大多数情况下,DOM解析器不是在Android上解析XML的最佳选择。

There are class of parsers that don't need to load a document up front. 有一类解析器不需要预先加载文档。 These parsers are stream-based , which means they process an XML document while still reading it from the data source (the Web or a disk). 这些解析器是基于流的 ,这意味着它们处理XML文档,同时仍然从数据源(Web或磁盘)读取它。 This implies that you do not have random access to the XML tree as with DOM because no internal representation of the document is being maintained. 这意味着您没有像DOM一样随机访问XML树,因为没有维护文档的内部表示。 Stream parsers can be further distinguished from each other. 流解析器可以进一步彼此区分。 There are push parsers that, while streaming the document, will call back to your application when encountering a new element. 有一些推送解析器 ,在流式传输文档时,会在遇到新元素时回调您的应用程序。 SAX parsers, fall into this class. SAX解析器属于这个类。 Then there are pull parsers, which are more like iterators or cursors: here the client must explicitly ask for the next element to be retrieved. 然后有拉解析器,更像是迭代器或游标:这里客户端必须明确要求检索下一个元素。

Source: Android in Practice. 资料来源:Android实践。

The SAX XML Parser already available into the Android SDK. SAX XML Parser已经可以在Android SDK中使用。

http://developer.android.com/reference/org/xml/sax/XMLReader.html http://developer.android.com/reference/org/xml/sax/XMLReader.html

so it is easy to access. 所以它很容易访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM