Reputation: 1516
Android device is much slower and have much lower memory compare to PC/server, So what is the best way to handling XML in Android? And I have a set of very complex xml needed to parse. both SAX or DOM will cause too much code. Anybody have good suggestion? I want to make it clean and fast
Upvotes: 3
Views: 3567
Reputation: 14505
XMLPullParser looks like best option available. Check Quick Introduction to XmlPull v1 API.
Also have a look at vtd-xml. As per them from their home page,
Following link also has various options you can use:
http://www.ibm.com/developerworks/xml/library/x-android/
Upvotes: 1
Reputation: 24472
Your best bet is SAX or XMLPull. Android provides API for both. The main difference here is:
Below is an example of XmlPull parsing:
try {
reader = new InputStreamReader(...from soem input stream);
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(reader);
parser.require(XmlPullParser.START_DOCUMENT, null, null);
// get the event type
int eventType = parser.getEventType();
// see what type of event it is...
while(eventType != XmlPullParser.END_DOCUMENT) {
String pName = parser.getName();
switch(eventType) {
case XmlPullParser.START_TAG:
if(pName.equals("sometag")) {
// get the textcontent
String msg = parser.nextText();
// get attribute value
String strErrCode = parser.getAttributeValue(null, "somattr");
break;
case XmlPullParser.END_TAG:
if(pName.equals("sometag")) {
// do something
}
break;
default:
break;
}
eventType = parser.next(); // parse next and generate event
} // while loop
}catch(Exception e) {
String msg = e.getMessage();
Log.e(TAG, "Error while parsing response: " + msg, e);
}
Here is a quick intro on how to do pull parsing
Upvotes: 1
Reputation: 22920
The Kind of parser you use in your application depends on your requirement. You can try XMLPullParser
too. You can see the performance of all the three parsers here..
http://www.developer.com/ws/article.php/10927_3824221_2/Android-XML-Parser-Performance.htm
There are a few third party XML parsers available too... I was using this parser for one of my previous application and it was fairly fast. It has Xpath implementation in it.
http://vtd-xml.sourceforge.net/
Upvotes: 2
Reputation: 114787
Don't care too much about the size of the class files ("the code"), care about the memory consumption of the application. For android, it could be advisable to implement a SAX parser and extract only the information needed to an internal data model.
A DOM builder will create a document for the complete XML document in memory and that might cause performance problems.
Upvotes: 1