How to solve the XML parsing performance issue on Android How to solve the XML parsing performance issue on Android android android

How to solve the XML parsing performance issue on Android


Original answer, in 2012

(note: make sure you read the 2016 update below!)

I just did some perf testing comparing parsers on Android (and other platforms). The XML file being parsed is only 500 lines or so (its a Twitter search Atom feed), but Pull and DOM parsing can churn through about 5 such documents a second on a Samsung Galaxy S2 or Motorola Xoom2. SimpleXML (pink in the chart) as used by the OP ties for slowest with DOM parsing.

SAX Parsing is an order of magnitude faster on both of my Android devices, managing 40 docs/sec single-threaded, and 65+/sec multi-threaded.

Android 2.3.4:

performance comparison of xml parsing methods on Android

The code is available in github, and a discussion here.

Update 18th March 2016

OK, so its been almost 4 years and the world has moved on. I finally got around to re-running the tests on:

  1. A Samsung Galaxy S3 running Android 4.1.2
  2. A Nexus7 (2012) running Android 4.4.4
  3. A Nexus5 running Android 6.0.1

Somewhere between Android 4.4.4 and Android 6.0.1 the situation changed drastically and we have a new winner: Pull Parsing FTW at more than twice the throughput of SAX. Unfortunately I don't know exactly when this change arrived as I don't have any devices running Android > 4.4.4 and < 6.0.1.

Android 4.1.2:

performance comparison of xml parsing methods on Android 4.1.2

Android 4.4.4:

performance comparison of xml parsing methods on Android 4.4.4

Android 6.0.1:

performance comparison of xml parsing methods on Android 6.0.1


I think the best way to work with XML on Android is use VDT-XML library

My XML file contains more then 60 000 lines and VDT-XML handles it as following:

Nexus 5 : 2055 millisec

Galaxy Note 4 : 2498 milisec

You can find more benchmark reports by link : VTD-XML Benchmark

Short example of XML file

 <database name="products">        <table name="category">            <column name="catId">20</column>            <column name="catName">Fruit</column>        </table>        <table name="category">            <column name="catId">31</column>            <column name="catName">Vegetables</column>        </table>        <table name="category">            <column name="catId">45</column>            <column name="catName">Rice</column>        </table>        <table name="category">            <column name="catId">50</column>            <column name="catName">Potatoes</column>        </table></database>

Configuration of "build.gradle" file

dependencies {    compile files('libs/vtd-xml.jar')}

Source code example:

import com.ximpleware.AutoPilot;import com.ximpleware.VTDGen;import com.ximpleware.VTDNav;String fileName = "products.xml";VTDGen vg = new VTDGen();if (vg.parseFile(fileName, true)) {     VTDNav vn = vg.getNav();     AutoPilot table = new AutoPilot(vn);     table.selectXPath("database/table");     while (table.iterate()) {        String tableName = vn.toString(vn.getAttrVal("name"));        if (tableName.equals("category")) {            AutoPilot column = new AutoPilot(vn);            column.selectElement("column");            while (column.iterate()) {                 String text = vn.toNormalizedString(vn.getText());                 String name = vn.toString(vn.getAttrVal("name"));                 if (name.equals("catId")) {                    Log.d("Category ID = " + text);                 } else if (name.equals("catName")) {                    Log.d("Category Name = " + text);                 }             }        }     }}

Result

Category ID = 20Category Name = FruitCategory ID = 31Category Name = VegetablesCategory ID = 45Category Name = RiceCategory ID = 50Category Name = Potatoes

it works for me and hope it helps you.


Using the SAX parser, I can parse a 15,000-line XML file in around 10 seconds on my HTC Desire. I suspect there is some other issue involved.

Are you populating a database from the XML? If so, are you remembering to wrap your entire parse operation in a DB transaction? That alone can speed things up by an order of magnitude.