为什么SAX XML Parser太慢了

时间:2013-11-26 10:00:05

标签: android xml-parsing android-asynctask

我使用webservice创建了一个xml文件并创建了一个ASYNC来下载,然后使用SAXXMLParser解析它。 但要做到这一点需要很长时间。目前我有2500行和10列数据。大约需要5分钟。这是非常不可接受的。 这是我的代码

      public class AsyncStock  extends AsyncTask<Void, Void, Void> {
 List<Stock> mystocks = null;
Context mcontext;
ProgressDialog pd;
int x=0;
public AsyncStock(Context context) {
    super();
    this.mcontext = context;
}
@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub

    try{
        URLConnection conn = new URL("http://192.168.1.2/mob2server.asmx/GetStock").openConnection();

        mystocks = SAXXMLParser.parse(conn.getInputStream());

     DatabaseHandlerStock dbc;
     dbc=new DatabaseHandlerStock(mcontext);
     String  category,  make,  model, productcode,  productname,  smallimages, largeimages,  description,  mrp,  unit, pkg;
     for(Stock in :mystocks)

        {
         x++;
            category=in.getCategory();
            make=in.getMake();
            model=in.getModel();
            productcode=in.getProductcode();
            productname=in.getProductname();
            smallimages=in.getSmallimages();
            largeimages=in.getLargeimages();
            description=in.getDescription();
            mrp=in.getMrp();
            unit=in.getUnit();
            pkg=in.getPkg();

            dbc.addContact(new Catalogue(category.replace("$","&"),  make.replace("$","&"),  model.replace("$","&"), productcode,  productname.replace("$","&"),  smallimages, largeimages,  description.replace("$","&"),  mrp,  unit, pkg));
        }
    dbc.close();
      }
        catch (Exception e) 
        {
            System.out.println(e.getMessage());
        }
    return null;
}
@Override
protected void onPostExecute(Void result) {
    pd.cancel();
     Toast.makeText(mcontext, "Total " + x + " Models Imported!", Toast.LENGTH_LONG).show();
    super.onPostExecute(result);


}

@Override
protected void onPreExecute() {
    pd=new ProgressDialog(mcontext);
    pd.setMessage("Please Wait. We are Downloading the New Catalogue...");
    pd.show();
    pd.setCancelable(false);
    pd.setCanceledOnTouchOutside(false);
    super.onPreExecute();
}

}

这是Parser Class

public class StockParse extends DefaultHandler {

private List<Stock> stocks;
private String tempVal;
private Stock tempstock;

public StockParse() {
    stocks = new ArrayList<Stock>();
}

public List<Stock> getStocks() {
    return stocks;
}

// Event Handlers
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    // reset
    tempVal = "";
    if (qName.equalsIgnoreCase("Table")) {
        // create a new instance of employee
        tempstock = new Stock();
    }
}

public void characters(char[] ch, int start, int length)
        throws SAXException {
 //   tempVal = new String(ch, start, length);
    if(tempVal == null)
        tempVal = new String(ch, start, length);
      else
          tempVal += new String(ch, start, length);
}

public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (qName.equalsIgnoreCase("Table"))
    {
        // add it to the list
        stocks.add(tempstock);
    }
    else if (qName.equalsIgnoreCase("NAVIN"))
    {
        //tempstock.setCategory(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKCATEGORY"))
    {
        tempstock.setCategory(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKMAKE"))
    {
        tempstock.setMake(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKMODEL"))
    {
        tempstock.setModel(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKPRODUCTCODE"))
    {
        tempstock.setProductcode(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKPRODUCTNAME"))
    {
        tempstock.setProductname(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKSMALLIMAGES"))
    {
        tempstock.setSmallimages(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKLARGEIMAGES"))
    {
        tempstock.setLargeimages(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKDESCRIPTION"))
    {
        tempstock.setDescription(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKMRP"))
    {
        tempstock.setMrp(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKUNIT"))
    {
        tempstock.setUnit(tempVal);
    }
    else if (qName.equalsIgnoreCase("NKPKG"))
    {
        tempstock.setPkg(tempVal);
    }
}

}

       class Stock
      {   
   private String  category,  make,  model, productcode,  productname,  smallimages,    largeimages,  description,  mrp,  unit, pkg;

    public String getCategory() {
    return category;
}


public void setCategory(String category) {
    this.category = category;
}


public String getMake() {
    return make;
}

public void setMake(String make) {
    this.make = make;
}

public String getModel() {
    return model;
}


public void setModel(String model) {
    this.model = model;
}


public String getProductcode() {
    return productcode;
}

public void setProductcode(String productcode) {
    this.productcode = productcode;
}

public String getProductname() {
    return productname;
}

public void setProductname(String productname) {
    this.productname = productname;
}


public String getSmallimages() {
    return smallimages;
}


public void setSmallimages(String smallimages) {
    this.smallimages = smallimages;
}

public String getLargeimages() {
    return largeimages;
}


public void setLargeimages(String largeimages) {
    this.largeimages = largeimages;
}

public String getDescription() {
    return description;
}


public void setDescription(String description) {
    this.description = description;
}

public String getMrp() {
    return mrp;
}

public void setMrp(String mrp) {
    this.mrp = mrp;
}

public String getUnit() {
    return unit;
}

public void setUnit(String unit) {
    this.unit = unit;
}

public String getPkg() {
    return pkg;
}

public void setPkg(String pkg) {
    this.pkg = pkg;
}

请帮助我加快解析速度。因为我是新手。

1 个答案:

答案 0 :(得分:0)

Json比xml弱。有一次,我尝试比较它们,我看到一个Json文件比xml文件少2倍