开发者

What is the best way to handling XML in Android

Android device is much slower and have much lower memory compare to PC/server, So what is the best开发者_Python百科 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


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/


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.


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


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜