简体   繁体   中英

Android SAX parsing retrieval very slow

In my app i have some 50 to 100 data to be parsed...also some images within 2 MB size...All these are to be retrieved to a single activity and are sorted and displayed there...

But its taking about 2 minutes...thats too much.

What to do for reducing the parse time?

Or am i wrong using SAX parsing???Suggestions please....

SAXParserFactory spf = SAXParserFactory.newInstance();
      SAXParser sp = spf.newSAXParser();
      XMLReader xr = sp.getXMLReader();

      /** Send URL to parse XML Tags */
      URL sourceUrl = new URL(url);

      /** Create handler to handle XML Tags ( extends DefaultHandler ) */
      XmlHandler xmlHandler4Profile = new XmlHandler();
      xr.setContentHandler(xmlHandler4Profile);
      xr.parse(new InputSource(sourceUrl.openStream()));

      } 

Code for XmlHandler.java

public class XmlHandler extends DefaultHandler{
static GetXml getxml;
Boolean currentElement = false;
String currentValue = null;


@Override
public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
currentElement=true;
   if (localName.equals("root"))
    {
        /** Start */ 
       getxml = new GetXml();
    } 
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    // TODO Auto-generated method stub
    currentElement=false;

    //**************************display page************************

    if (localName.equalsIgnoreCase("DisplayId"))
         getxml.setDisplayId(currentValue);     

     if (localName.equalsIgnoreCase("DisplayPage"))
         getxml.setDisplayPage(currentValue);

    //**************************category details************************

        if (localName.equalsIgnoreCase("CategoryId"))
             getxml.setCategoryId(currentValue);        

         if (localName.equalsIgnoreCase("CategoryName"))
             getxml.setCategory(currentValue);

        //**************************news details************************ 

         if (localName.equalsIgnoreCase("Picture"))
             getxml.setPictureName(currentValue);       

         if (localName.equalsIgnoreCase("newsHeading"))
             getxml.setNewsHeading(currentValue);

         if (localName.equalsIgnoreCase("Mainnews"))
             getxml.setMainNews(currentValue);




}
    @Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    // TODO Auto-generated method stub
     if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }
}

You really should profile your code and find out where it's spending its time.

I can see one performance issue and one correctness issue in your posted code.

You'll gain a bit by adding some else usage in your endElement code. Once you know localName matches something there's no need to check it against all the later possibilities which can not possibly match.

That may not gain much, but it's certainly worth trying.

Your characters method is wrong, but this has to do with correctness rather than efficiency. See my answer to another question on SAX parsing for what's wrong and how to write a correct characters method. The change will also necessitate a change in how endElement gets the value, as it has to be collected into a buffer.

The answer is likely that the SAX parser you're using is checking the inte.net for the schema or DTD of your xml content. If you write an entityResolver that bypasses these checks, you'll likely improve your start time (which is often 2 minutes or so).

See this for more details.

How to read well formed XML in Java, but skip the schema?

This solved the problem for me using Xerces...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM