The Android SDK provides two Java XML parsers that most developers are accustomed to: DOM (Document Object Model) and SAX (Simple API for XML). A while back, the Android SDK was enhanced to include an XML Pull Parser, with a note that the change provided “higher performance for small memory applications”, such as portable devices.
I had initially assumed that meant the Pull parser would be the XML parser of choice for the Android platform. Otherwise, why would the developers of the Android platform include it so early on with such a change description? Before I began the testing, I was expecting the performance of the Pull parser to be the fastest of the three methods available. I had already begun using it as the parser of choice. But was it really the right choice? And, if so, how much faster was it? I had to know.
Understanding How the Different Parsers Work
A DOM parser works by parsing an XML file into a native data structure matching the hierarchy of the XML file. Most of the processing is done up front and the entire file is looked at, so a DOM parser typically uses the most memory of the three parsers. A SAX parser works by having the user implement a class with method handlers for various events, such as finding tags or attributes. A Pull Parser works by creating a loop that continually requests the next event and can then handle that event directly within the loop. The idea with the Pull Parser is that it can easily be stopped at any point, only do processing on demand, and remove the overhead of extra method calls and classes.
Although each of the three parsers is different, they all have the same fundamentals: they are designed for parsing XML. This means that the code for handling events, such as finding and interpreting the tag data and attributes, can remain basically the same across all three implementations. Since this code will remain the same, any performance differences should be due to differences in the parsing algorithms, including their use of memory, which may cause Java garbage collection to run more frequently.