How to speed up XML parsing on Android?

Good afternoon (depending on where you live)

I am new to Android development and am currently working on porting functionality from an existing iOS application to Android. Part of this function is the analysis of the "large" (~ 13,000 lines) RSS XML file, which contains about 500 entries. I spent somewhere 10-15 hours researching parsing XML files on Android. And try out the basic XML parsers: DOM, SAX and Pull-parsing. Here are my results running inside the emulator on my inbox (32-bit version of Windows Vista, 2.2 GHz dual-core processor, 3 GB of RAM):

SAX: ~ 6: 00 minutes

Pull-Parsing: ~ 4: 00 minutes

DOM: More than 4:00 minutes, but not the time when I had this implementation.

I also tried this RSS reader from github, but it took> 10:00 minutes:

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

The implementations for SAX, PP and DOM were taken from stackoverflow.com streams, so I feel pretty confident that I didnโ€™t do anything non-standard in them (although I donโ€™t), I decided to turn to a larger, more experienced crowd, so that get some ideas on what else i can try.

I have no control over the file format. If I implemented this pass-through, I would just write a web service that did all the hard work on the server and then send a small, compact JSON-serialized list. Instead, I have a 13K line file. :)

Any insight on what I can do? This seems to be a fairly common problem, but most of the answers just say to try one of the different core XML parsers. In my case, I tried all three, and even the fastest one seems too slow.

What am I doing wrong? Are there any usual โ€œnewbiesโ€ problems that people usually encounter when parsing XML over the network on Android ???

Thanks in advance for any help you can provide!

+4
source share
2 answers

Use vtd-xml .

Here are some tests.

In addition, the emulator is very slow, so try it on a real device and you will probably see big improvements.

+3
source

I agree with Jave's answer. Best Choice VDT-XML Library This example shows how you can use this library.

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> 

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.

+1
source

All Articles