简体   繁体   English

如何加速Android XML解析?

[英]How Can I Speed Up Android XML Parsing?

Good afternoon (depending on where you live)! 下午好(取决于你住的地方)!

I'm fairly new to Android development and I'm currently working on moving the functionality from an existing iOS app to Android. 我是Android开发的新手,我正在努力将功能从现有的iOS应用程序转移到Android。 Part of this functionality is to parse a "large" (~13,000 line) RSS XML file that contains about 500 entries. 此功能的一部分是解析包含大约500个条目的“大”(~13,000行)RSS XML文件。 I've spent anywhere from 10-15 hours researching XML parsing on Android AND trying out the major XML parsers: DOM, SAX, and Pull-parsing. 我花了10-15个小时在Android上研究XML解析并尝试使用主要的XML解析器:DOM,SAX和Pull-parsing。 Here are my results, running within' the emulator on my box (32-bit Windows Vista, 2.2 GHz Dual CPU, 3 GB RAM): 以下是我的结果,在我的盒子上的模拟器(32位Windows Vista,2.2 GHz双CPU,3 GB RAM)中运行:

SAX: ~6:00 minutes SAX:约6:00分钟

Pull-Parsing: ~4:00 minutes 拉解析:~4:00分钟

DOM: Greater than 4:00 minutes, but didn't time it when I had that implementation coded. DOM:大于4:00分钟,但是当我对该实现进行编码时没有计时。

I also tried this RSS Reader from github, but it took >10:00 minutes: 我也从github尝试过这个RSS阅读器,但花了> 10:00分钟:

https://github.com/matshofman/Android-RSS-Reader-Library https://github.com/matshofman/Android-RSS-Reader-Library

The implementations for the SAX, PP, and DOM were all taken from stackoverflow.com threads, so I feel fairly confident that I didn't do anything non-standard in them (though, I'm not ruling that out). SAX,PP和DOM的实现都是从stackoverflow.com线程中获取的,所以我相信我没有做任何非标准的事情(尽管如此,我并没有把它排除在外)。 I've decided to appeal to a larger, more experienced, crowd to get some ideas of what else I can try. 我决定吸引更多,更有经验的人群,以获得一些我可以尝试的想法。

I don't have any control over the format of the file. 我无法控制文件的格式。 If I was implementing this end-to-end, I would just write a Web Service that did all the heavy-lifting on the server and then sent down a small, compact, JSON-serialized list. 如果我实现这种端到端的方式,我只会编写一个Web服务,在服务器上完成所有繁重工作,然后发送一个小型,紧凑的JSON序列化列表。 Instead, I have the 13K line file. 相反,我有13K行文件。 :) :)

Any insight into what I can do? 对我能做什么的任何见解? It seems to be a fairly common problem, but most responses just say to try one of the different main XML parsers. 这似乎是一个相当普遍的问题,但大多数响应只是说尝试不同的主要XML解析器之一。 In my case, I've tried all three and even the fastest seems way too slow. 在我的情况下,我已经尝试了所有三个,甚至最快似乎太慢了。

What am I doing wrong? 我究竟做错了什么? Are there any normal "newbie" problems that people usually encounter when doing XML parsing over the network on Android??? 在Android上通过网络进行XML解析时,人们通常会遇到任何正常的“新手”问题吗?

Thank you in advance for any help you can provide! 提前感谢您提供的任何帮助!

Use vtd-xml . 使用vtd-xml

Here are some benchmarks. 这是一些基准。

Also, the emulator is extremely slow, so try it on a real device and you will likely see great improvements. 此外,模拟器非常慢,所以在真实设备上尝试它,你可能会看到很大的改进。

I agree with Jave's answer. 我同意Jave的回答。 The best choice is VDT-XML library This example demonstrate how you can use this library. 最好的选择是VDT-XML库这个例子演示了如何使用这个库。

XML file: 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>

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

            }
        }
     }
}

it works for me and hope it helps you. 它对我有用,希望它对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM