Android XML pull解析器速度慢,如何加速?

时间:2013-01-10 13:05:48

标签: android xml-parsing xmlpullparser

我使用XmlPullParser编写了一个XML解析器,但是解析几百行数据需要花费大量时间。

你能看看代码并告诉我我做错了什么吗?

    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;

基于这种结构,我还有三个解析器。 感谢。

1 个答案:

答案 0 :(得分:1)

XmlPullParser类处理大型XML文件非常慢。为实现此目标,我更喜欢使用VDT-XML libraryVTD-XML Benchmark

在我的情况下,我有66841行的XML文件,你可以查看我的结果:

Nexus 5:2055毫秒

Galaxy Note 4:2498 milisec

XML文件的简短示例

 <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>

“build.gradle”文件的配置

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

源代码示例:

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);
                 } 

            }
        }
     }
}

结果

Category ID = 20
Category Name = Fruit

Category ID = 31
Category Name = Vegetables

Category ID = 45
Category Name = Rice

Category ID = 50
Category Name = Potatoes

它对我有用,希望对你有所帮助。