I have to read a XML file with about ~4000 lines on Android. First I tried the SimpleXML library because it\'s the easiest and it took about 2 minutes on my HTC Desire. So I
I think the best way to work with XML on Android is use VDT-XML library
My XML file contains more then 60 000 lines and VDT-XML handles it as following:
Nexus 5 : 2055 millisec
Galaxy Note 4 : 2498 milisec
You can find more benchmark reports by link : VTD-XML Benchmark
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.
If you are parsing Dates within your XML that can significantly slow down your parsing. With the more recent versions of Android this becomes less of a problem (as they optimised the loading of timezone info)
If you have Dates that are being parsed and you don't need them, then you could use a SAX parser and ignore any of the Date elements.
Or if you can change your XML schema, consider storing the Dates as integers rather than formatted strings.
You mentioned you are making string comparisons, this can be pretty expensive as well. Perhaps consider using a HashMap for the strings you are comparing, this can give noticeable performance benifits.
we're using the pull-parser very effectively for 1MB XML Files - and they are read in about 10-20 Seconds on my Desire. So if your code is okay, the speed will be as well. It's obvious that DOM is very slow on a limited memory environment, but pull or SAX really aren't
It's very hard to tell you why your code is slow without seeing your code, and it's very hard to believe your assertion that the slowness is due to the XML parser when you haven't provided details of any measurements to prove this.
Using the SAX parser, I can parse a 15,000-line XML file in around 10 seconds on my HTC Desire. I suspect there is some other issue involved.
Are you populating a database from the XML? If so, are you remembering to wrap your entire parse operation in a DB transaction? That alone can speed things up by an order of magnitude.
If your parsing from a Socket its the I/O thats taking the time, not the parsing. Try consume the data first, then parse once loaded and measure the performance. If the file is too big then consider a BufferedInputStream with a very large buffer, this should improve performance for you.
I very seriously doubt Simple XML is going to take 2 minutes to load 4000 lines, I realise a handset is going to be a lot slower than a workstation, however I can load 200,000 lines of XML in 600ms on my workstation.