2012-06-26 3 views
1

저는 실제로 처음으로 SAX 파서를 사용하고 있습니다. 자습서와 질문을 통해 대부분의 것을 파악할 수 있었고 이제 거의 모든 것이 잘 작동합니다. 그러나 나는 여전히 한 가지 문제가 있습니다. logcat을 구문 분석 할 때 많은 가비지 수집기 로그가 표시됩니다. 그리고 때로는 힙 메시지도 생성됩니다. 그리고 이런 일이 발생하면 항상 잘못된 항목이 생성됩니다. 여기에 누군가가 최적화 제안을 한 것 같습니다.안드로이드에서 SAX 파서의 메모리 문제가 발생했습니다.

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

및 구문 분석이 시작되는 부분 :

나중에 모든 항목이 sqlite가 테이블에 저장됩니다 발견, 핸들러의 문자 방법의 코드는, 파서가 항상 전체 파일을 구문 분석이다 시도해보십시오 { URL src = 새 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()); 
    } 

모든 제안에 감사드립니다.

+0

코드에서 수행해야 할 작업은 무엇입니까? 당신이 게시 한 것은'else else if else' 문장의 묶음입니다. SAX 파서 라이브러리 사용에 대해 생각해 보셨습니까? 안드로이드는 하나의 내장 ... http : //developer.android.com/reference/android/sax/package-summary.html – Squonk

+0

죄송합니다, 이것은 성능 문제를 일으키는 부분이 될 것이라고 생각했습니다. 핸들러를 생성하는 파서 클래스가 있으므로 위에 코드를 추가했습니다. 그리고 핸들러는 항목의 arraylist를 만듭니다. 시작/끝 요소 메서드에서 나는 bool 변수를 설정하고 생성 된 항목을 목록에 추가 한 다음 나중에 해제합니다. 아니요,이 라이브러리에 대해 전혀 생각하지 않았습니다. 모든 튜토리얼처럼 진행되었지만 사용법을 살펴볼 것입니다. –

답변

0

문자 메소드와 함께 startDocument, startElements, endElements, endDocuments와 같은 올바른 메소드를 무시하여 모든 코드를 작성하면 안됩니다.

this 예를 참조하십시오.

+0

미안하지만, 나는 당신이 의미하는 것을 볼 수 없다. 필자의 견해로 볼 때이 예제는 내가 가지고있는 것과 유사 해 보인다. 위에서 언급 한 것처럼,리스트가 생성되는 StartDocument 메소드, startElement 메소드, bool이 true로 설정된 endElement 메소드, bool false로 설정됩니다. –

+0

타고 "문자"방법 만 있습니다. 최소한 startDocument 및 startElements 메소드를 대체해야합니다. 링크를주의 깊게 참조하십시오. – Rajnikant

+0

죄송 합니다만, 이번에는 약간의 부정확 한 것이 었습니다. 문자 메서드 만 보여 주면 완성 된 메서드를 붙여도 너무 길어질 것이라고 생각했습니다. 그리고 나는 또한 문자 메서드에서 다른 것들보다 실수를 찾는 것이 훨씬 더 가능성이 있다고 생각했습니다. –

관련 문제