tags:

views:

79

answers:

4

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

+1  A: 

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.

Andreas_D
I had to care about the size of code since the xml format may change in feature and I had to worry about maintain cost
Jammy Lee
I understand - I thought you wanted to keep the code base small because the application is designd for android mobiles.
Andreas_D
+1  A: 

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/

Rahul
A: 

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,

  • The world's most memory-efficient (1.3x~1.5x the size of an XML document) random-access XML parser.
  • The world's fastest XML parser: On a Core2 2.5Ghz Laptop, VTD-XML outperforms DOM parsers by 5x~12x, delivering 90~120 MB/sec per core sustained throughput.
  • The world's fastest XPath 1.0 implementation.
  • The world's most efficient XML indexer that seamlessly integrates with your XML applications.
  • The world's only incremental-update capable XML parser capable of cutting, pasting, splitting and assembling XML documents with max efficiency.
  • The world's only XML parser that allows you to use XPath to process 256 GB XML documents.

Following link also has various options you can use:

http://www.ibm.com/developerworks/xml/library/x-android/

YoK
A: 

Your best bet is SAX or XMLPull. Android provides API for both. The main difference here is:

  • In SAX, the parser drives the parsing and does callbacks on your code
  • In pull parsing the user code drives the parsing.

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

naikus
pull parse saved a little of code, but the code base still be big since the structure of the xml is so complex
Jammy Lee