Android中SAX解析器的内存问题

Android中SAX解析器的内存问题,android,saxparser,Android,Saxparser,实际上,我第一次使用的是SAX解析器。从这里的教程和问题中,我可以了解到大多数事情,现在几乎所有事情都很顺利。但我还有一个问题。解析logcat时,会显示许多垃圾收集器日志。有时还会出现堆消息增长。如果发生这种情况,总是会创建错误的项目。 也许这里有人有一些优化建议 这是处理程序的characters方法的代码,解析器总是解析完整的文件,然后所有找到的项都存储在sqlite表中: public void characters(char ch[], int start, int length) {

实际上,我第一次使用的是SAX解析器。从这里的教程和问题中,我可以了解到大多数事情,现在几乎所有事情都很顺利。但我还有一个问题。解析logcat时,会显示许多垃圾收集器日志。有时还会出现堆消息增长。如果发生这种情况,总是会创建错误的项目。 也许这里有人有一些优化建议

这是处理程序的characters方法的代码,解析器总是解析完整的文件,然后所有找到的项都存储在sqlite表中:

public void characters(char ch[], int start, int length) {
    //decide wich tag is active, process string

    String parse = new String(ch, start, length);

    if(in_starttag)
    {
        currentItem = new Item();
        in_starttag = false;
    }
    else if(in_mainclasstag)
    {
        if(cats.indexOf(parse) == -1)
        {
            cats.add(parse);

            currentMain = new EgrohItem((categories.size()+1), parse, 1, -1);
            categories.add(currentMain);
        }
        else
        {
            currentMain = categories.get(cats.indexOf(parse));
        }
    }
    else if(in_midclasstag)
    {
        if(cats.indexOf(parse) == -1)
        {
            cats.add(parse);
            currentMid = new EgrohItem((categories.size()+1), parse, 0, currentMain.getId());
            categories.add(currentMid);
        }
        else
        {
            currentMid = categories.get(cats.indexOf(parse));
        }
    }
    else if(in_subclasstag)
    {
        if(cats.indexOf(parse) == -1)
        {
            cats.add(parse);
            currentSub = new EgrohItem((categories.size()+1), parse, 0, currentMid.getId());
            categories.add(currentSub);
        }
        else
        {
            currentSub = categories.get(cats.indexOf(parse));
        }
        currentItem.setAbove_cat(currentMain.getId());
    }
    else if(in_idtag)
    {
        currentItem.setArt_nr(parse);
    }
    else if(in_destag1)
    {
        if(currentItem.getName() != null)
        {
            currentItem.setName(currentItem.getName() + parse);
        }
        else currentItem.setName(parse);
    }
    else if(in_destag2)
    {
        if(currentItem.getName() != null)
        {
            currentItem.setName(currentItem.getName() + parse);
        }
        else currentItem.setName(parse);
    }
    else if(in_destag3)
    {
        if(currentItem.getName() != null)
        {
            currentItem.setName(currentItem.getName() + parse);
        }
        else currentItem.setName(parse);
    }
    else if(in_destag4)
    {
        if(currentItem.getName() != null)
        {
            currentItem.setName(currentItem.getName() + parse);
        }
        else currentItem.setName(parse);
    }
    else if(in_descriptag)
    {
        if(currentItem.getDescription() != null)
        {
            String des = currentItem.getDescription()+" "+parse;
            currentItem.setDescription(des);
        }
        else currentItem.setDescription(parse);
    }
    else if(in_eantag)
    {
        currentItem.setEan(parse);
    }
    else if(in_suppnrtag)
    {
        currentItem.setSupp_nr(parse);
    }
    else if(in_supptag)
    {
        currentItem.setSupplier(parse);
    }
    else if(in_acctag1)
    {
        currentAcc_Nr = parse;
        currentItem.setAccessories_number(currentAcc_Nr);
    }
    else if(in_acctag2)
    {
        if(parse.length()>0 && !parse.equals(" "))
        {
            currentAcc_Nr += parse;
        }
        currentItem.setAccessories_number(currentAcc_Nr);
    }
    else if(in_acctag3)
    {
        if(parse.length()>0 && !parse.equals(" "))
        {
            currentAcc_Nr += parse;
        }
        currentItem.setAccessories_number(currentAcc_Nr);
    }
    else if(in_acctag4)
    {
        if(parse.length()>0 && !parse.equals(" "))
        {
            currentAcc_Nr += parse;
        }
        currentItem.setAccessories_number(currentAcc_Nr);
    }
    else if(in_amount1tag)
    {
        Integer amo1 = new Integer(parse);
        currentItem.setAmo1(amo1);
    }
    else if(in_amount2tag)
    {
        Integer amo2 = new Integer(parse);
        if(amo2 > 0) currentItem.setAmo2(amo2);
    }
    else if(in_amount3tag)
    {
        Integer amo3 = new Integer(parse);
        if(amo3 > 0) currentItem.setAmo3(amo3);
    }
    else if(in_price1tag)
    {
        currentItem.setPrice1(parse);
    }
    else if(in_price2tag)
    {
        if(!parse.equals("0")) currentItem.setPrice2(parse);
    }
    else if(in_price3tag)
    {
        if(!parse.equals("0")) currentItem.setPrice3(parse);
    }
    else if(in_mctag)
    {
        categories.get(currentMain.getId()-1).setName(parse);
    }
    else if(in_mictag)
    {
        categories.get(currentMid.getId()-1).setName(parse);
    }
}
以及开始解析的部分: 尝试 { URL src=新URL(src_URL); SAXParserFactory spf=SAXParserFactory.newInstance(); SAXParser sp=spf.newSAXParser()


非常感谢您的每一个建议!

您不能通过使用正确的方法编写所有代码,如startDocument、startElements、endElements、endDocuments以及characters方法


请参阅示例。

您的代码应该做什么?您发布的只是一堆
if-else if-else
语句。您是否考虑过使用SAX解析器库?Android内置了一个…对不起,我以为这会导致性能问题。我有一个解析器类来创建处理程序,因此,我已经添加了上面的代码。处理程序创建了一个项目的arraylist。在开始/结束元素方法中,我只是设置bool变量,将创建的项目添加到列表中,然后发布。不,从来没有想过这个库,只是按照所有教程的方式来做,但是我会看看如何使用它。对不起,我看不到,什么你的意思是。从我的角度来看,这个例子看起来和我的相似…就像上面提到的,我有一个创建列表的StartDocument方法,一个startElement方法,其中bools设置为true,还有一个endElement方法,其中bools设置为false。你只有超过的“字符”方法。至少你需要重写startDocument和startElements方法。请仔细参考链接。对不起,这次我可能有点不精确,只显示了字符方法,我认为如果粘贴完整的方法会太长。我还认为在字符中发现错误的可能性更大他们的方法比其他方法都好。
        XMLReader xr = sp.getXMLReader();
        MedExampleHandler meh = new MedExampleHandler(prefcon);
        xr.setContentHandler(meh);
        BufferedInputStream bis = new BufferedInputStream(src.openStream(), 200);
        InputSource is = new InputSource(bis);
        xr.parse(is);

        categories = meh.getCategories();
    }
    catch (Exception e)
    {
        Log.d(EgrohCatalogue.TAG, e.toString());
    }