2

我使用 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;

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

4

1 回答 1

1

XmlPullParser 类处理大型 XML 文件的速度非常慢。为此,我更喜欢使用VDT-XML 库VTD-XML Benchmark

就我而言,我有 66841 行的 XML 文件,您可以查看我的结果:

Nexus 5:2055 毫秒

Galaxy Note 4:2498 毫秒

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

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

于 2015-04-09T15:41:41.587 回答