iceman
iceman

Reputation: 826

Android XML pull parser slow, how to speed up?

I wrote an XML parser, using XmlPullParser, however it takes a lot of time to parse a few hundred lines of data.

Can you take a look at the code and tell me what am I doing wrong?

    case ONE:
            buffer.clear();
            xpp.setInput(responseBuffer[ONE], "UTF_8");
            // Returns the type of current event: START_TAG, END_TAG, etc..
            eventType = xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (xpp.getName().equalsIgnoreCase("firstTag")) {
                        bufferA = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("secondTag")) {
                        bufferB = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("thirdTag")) {
                        bufferC = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("u")) {
                        bufferD = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("n")) {
                        bufferE = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("d")) {
                        bufferF = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("so")) {
                        bufferG = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("wei")) {
                        bufferH = xpp.nextText();
                    } else if (xpp.getName().equalsIgnoreCase("ter")) {
                        bufferI = xpp.nextText();
                    }
                } else if (eventType == XmlPullParser.END_TAG
                        && xpp.getName().equalsIgnoreCase("close")) {
                    buffer.add(new VisitInfo(
                            bufferA,
                            bufferB,
                            bufferC,
                            bufferD, bufferE,
                            bufferF, bufferG,
                            bufferH, bufferI));
                }
                eventType = xpp.next(); // move to next element
            }

            break;

I have three more parsers, based on this structure. Thanks.

Upvotes: 2

Views: 1641

Answers (1)

Maksym Kalin
Maksym Kalin

Reputation: 1713

The XmlPullParser class is very slow to handle large XML files. For this goal I prefer to use VDT-XML library, VTD-XML Benchmark.

In my case I have XML file with 66841 lines and you can look at my results:

Nexus 5 : 2055 millisec

Galaxy Note 4 : 2498 milisec

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 = 20
Category Name = Fruit

Category ID = 31
Category Name = Vegetables

Category ID = 45
Category Name = Rice

Category ID = 50
Category Name = Potatoes

it works for me and hope it helps you.

Upvotes: 1

Related Questions