Friday, 13 October 2017

saxparser - Memory issue with SAX Parser in Android

I'm actually using SAX Parser the first time. From
tutorials and questions here I could figure most things out, nearly everything is
working fine now. But I have still one problems. When parsing logcat shows me many
Garbage-Collector Logs. And sometimes there occur also grow heap messages. And if this
happens, there are always wrong items created.
Perhaps someone here has some
optimization suggestions.



This is the code of
the handler's characters method, the parser always parses the complete file, afterwards
all found items are stored in a sqlite
table:



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

}

}


and
the part where the parsing is started:
try
{
URL src =
new URL(src_url);
SAXParserFactory spf =
SAXParserFactory.newInstance();
SAXParser sp =
spf.newSAXParser();





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

}


Thank you very much
for every suggestion!

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...