Android: DOM vs SAX vs. XMLPullParser parsing?

I am parsing an XML document using SAX Parser.

I want to know what is better and faster to work with DOM , SAX Parser or XMLPullParser .

+8
java android xml xml-parsing
source share
3 answers

it depends on what you are doing, if you have very large files, then you should use the SAX parser, since it will trigger events and release them, nothing is stored in memory, and with the help of the SAX analyzer you cannot access there is no return to the element in a random way !, but Dom allows you to access any part of the xml file, since it saves the entire file / document in memory. I hope that you ask this answer.

if you want to know which fastest Xerces parser will be the fastest you will find, and the SAX parser should give you more performance than Dom

+5
source share

The SAX XML parser is already available in the Android SDK.

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

therefore it is easy to obtain.

+1
source share

One aspect by which various types of parsers can be classified is whether they need to load the entire XML document into memory in front. Parsers based on the Document Object Model (DOM) do this: they parse XML documents into a tree structure, which can then be moved in memory to read its contents. This allows you to move the document in random order and provides some useful APIs that can be removed on top of the DOM, such as XPath, a path query language that was specifically designed to extract information from trees. Using only the DOM is not very useful, because its API is inconvenient, and it is expensive to always read everything in memory, even if you do not need it. Therefore, DOM parsers are in most cases not the best choice for XML parsing on Android.

There is a class of parsers that do not need to load a document in front. These parsers are stream-based, which means that they process the XML document while still reading it from a data source (Web or disk). It follows that you do not have random access to the XML tree, as to the DOM, because the internal representation of the document is not supported. The flow of parsers can be distinguished from each other. There are push parsers that when streaming a document access your application when they encounter a new element. SAX, fall into this class. Then there are pull parsers that look more like iterators or cursors: here the client must explicitly request that the next item be restored.

Source: Android in practice.

+1
source share

All Articles