What is the best way to handle XML in Android?

An Android device is much slower and has much less memory compared to a PC / server. So what is the best way to process XML in Android? And I have a set of very complex xml needed for parsing. both SAX or DOM will cause too much code. Anyone have a good suggestion? I want to make it clean and fast

+3
source share
4 answers

The type of parser that you use in the application depends on your requirement. You can try XMLPullParsertoo. Here you can see the performance of all three parsers.

http://www.developer.com/ws/article.php/10927_3824221_2/Android-XML-Parser-Performance.htm

There are several third-party XML parsers ... I used this parser for one of my previous applications, and it was pretty fast. It implements the implementation of Xpath.

http://vtd-xml.sourceforge.net/

+2
source

Do not care about the size of class files ("code"), worry about memory consumption in the application. For android, it may be appropriate to implement the SAX analyzer and extract only the information needed for the internal data model.

The DOM creator will create the document for the complete XML document in memory and may cause performance problems.

+1
source

XMLPullParser . API XmlPull v1.

vtd-xml. ,

  • (1.3x ~ 1.5x XML ) XML- .
  • XML- : Core2 2.5Ghz, VTD-XML DOM 5x ~ 12x, 90 ~ 120 / .
  • XPath 1.0 .
  • XML , XML-.
  • XML-, , , XML- . .
  • XML- , XPath 256 XML-.

, :

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

+1
source

Best of all is SAX or XMLPull . Android provides an API for both. The main difference is here:

  • In SAX, the parser controls parsing and calls back on your code
  • When parsing code, custom code controls parsing.

The following is an example parsing of XmlPull:

  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);
  }

Below is a brief introduction on how to perform traction parsing.

+1
source

Source: https://habr.com/ru/post/1762805/


All Articles