2011-07-27 4 views
0

나는 ExampleHandler extends from DefaultHandler이라는 클래스가 있습니다.안드로이드 Xml 처리기는 내 XML 파일의 마지막 객체 만 유지합니다.

이 클래스는 (웹) 외부 XML 파일에서 데이터를 읽고이 XML 파일의 구조는 다음과 같습니다

<resultsSet> 
    <result> 
     <title>WinRANI Web Services!</title> 
     <nom>DADI</nom> 
     <prenom>Morad</prenom> 
     <adresse>DANS MES REVES</adresse> 
    </result> 
... 
</resultsSet> 

나는 ParsedExampleDataSet라는이 XML 파일의 동일한 구조 (와 클래스를 만들었습니다 그것은 getters와 setter 있습니다).

'결과'개체를 모두 포함하는 ArrayList을 만들었지 만 처리기가 모든 개체를 읽을 때 ArrayList의 모든 항목이 동일하다는 문제가 있습니다.

package com.example.helloandroid; 

import java.util.ArrayList; 

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

import android.util.Log; 


public class ExampleHandler extends DefaultHandler{ 

    // =========================================================== 
    // Fields 
    // =========================================================== 

    private boolean in_resultset = false; 
    private boolean in_result = false; 
    private boolean in_title = false; 
    private boolean in_nom = false; 
    private boolean in_prenom = false; 
    private boolean in_adresse = false; 
    private boolean in_tel = false; 
    private boolean in_fax = false; 
    private boolean in_lon = false; 
    private boolean in_lat = false; 
    private boolean in_description = false; 
    private boolean in_infos = false; 

    private test t; 
    private int currentIndex = 0; 
    ParsedExampleDataSet[] p = new ParsedExampleDataSet[5]; 

    private ArrayList<ParsedExampleDataSet> myParsedExampleDataSetList = new ArrayList<ParsedExampleDataSet>(); 
    private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet(); 

    private ParsedExampleDataSet s1; 
    private ParsedExampleDataSet s2; 
    private ParsedExampleDataSet s3; 
    private ParsedExampleDataSet s4; 
    private ParsedExampleDataSet s5; 


    // =========================================================== 
    // Getter & Setter 
    // =========================================================== 

    public ArrayList<ParsedExampleDataSet> getParsedData() { 
      return this.myParsedExampleDataSetList; 
    } 

    // =========================================================== 
    // Methods 
    // =========================================================== 
    @Override 
    public void startDocument() throws SAXException { 
      this.myParsedExampleDataSet = new ParsedExampleDataSet(); 
    } 

    @Override 
    public void endDocument() throws SAXException { 
      // Nothing to do 
    } 

    /** Gets be called on opening tags like: 
    * <tag> 
    * Can provide attribute(s), when xml was like: 
    * <tag attribute="attributeValue">*/ 
    @Override 
    public void startElement(String namespaceURI, String localName, 
        String qName, Attributes atts) throws SAXException { 
      if (localName.equals("resultset")) { 
        this.in_resultset = true; 
      }else if (localName.equals("result")) { 
        this.in_result = true; 
      }else if (localName.equals("title")) { 
        this.in_title = true; 
      }else if (localName.equals("nom")) { 
        // Extract an Attribute 
        //String attrValue = atts.getValue("thenumber"); 
        //int i = Integer.parseInt(attrValue); 
        //myParsedExampleDataSet.setExtractedInt(i); 
        this.in_nom = true; 
      }else if (localName.equals("prenom")) { 
        this.in_prenom = true; 
      }else if (localName.equals("tel")) { 
        this.in_tel = true; 
      }else if (localName.equals("fax")) { 
        this.in_fax = true; 
      }else if (localName.equals("lon")) { 
        this.in_lon = true; 
      }else if (localName.equals("lat")) { 
        this.in_lat = true; 
      }else if (localName.equals("description")) { 
        this.in_description = true; 
      }else if (localName.equals("infos")) { 
        this.in_infos = true; 
      } 
    } 

    /** Gets be called on closing tags like: 
    * </tag> */ 
    @Override 
    public void endElement(String namespaceURI, String localName, String qName) 
        throws SAXException { 
      if (localName.equals("resultset")) { 
        this.in_resultset = false; 

      }else if (localName.equals("result")) { 

        this.in_result = false; 
        myParsedExampleDataSetList.add(myParsedExampleDataSet); 

      }else if (localName.equals("title")) { 
        this.in_title = false; 
      }else if (localName.equals("nom")) { 
        // Nothing to do here 
        this.in_nom = false; 
      }else if (localName.equals("prenom")) { 
        this.in_prenom = false; 
      }else if (localName.equals("tel")) { 
        this.in_tel = false; 
      }else if (localName.equals("fax")) { 
        this.in_fax = false; 
      }else if (localName.equals("lon")) { 
        this.in_lon = false; 
      }else if (localName.equals("lat")) { 
        this.in_lat = false; 
      }else if (localName.equals("description")) { 
        this.in_description = false; 
      }else if (localName.equals("infos")) { 
        this.in_infos = false; 
     } 
    } 

    /** Gets be called on the following structure: 
    * <tag>characters</tag> */ 
    @Override 
public void characters(char ch[], int start, int length) { 
      if(this.in_title){ 
      myParsedExampleDataSet.setExtractedTitle(new String(ch, start, length)); 
    }else if (this.in_nom){ 
      myParsedExampleDataSet.setExtractedNom(new String(ch,start, length)); 
    }else if (this.in_prenom){ 
      myParsedExampleDataSet.setExtractedPrenom(new String(ch,start, length)); 
    }else if (this.in_tel){ 
      myParsedExampleDataSet.setExtractedTel(new String(ch,start, length)); 
    }else if (this.in_fax){ 
      myParsedExampleDataSet.setExtractedFax(new String(ch,start, length)); 
    }else if (this.in_lon){ 
      myParsedExampleDataSet.setExtractedLon(new String(ch,start, length)); 
    }else if (this.in_lat){ 
      myParsedExampleDataSet.setExtractedLat(new String(ch,start, length)); 
    }else if (this.in_description){ 
      myParsedExampleDataSet.setExtractedDescription(new String(ch,start, length)); 
    }else if (this.in_infos){ 
      myParsedExampleDataSet.setExtractedInfos(new String(ch,start, length)); 
    } 

} 
} 

내 xml 파일도 다른 필드가 있습니다

여기 내 코드입니다.

답변

1

이것은 Java의 일반적인 오해입니다. 자바는 모든 비 - 프리미티브를 포인터로 취급한다는 것을 기억해야한다. 귀하의 경우에는 데이터 컨테이너 개체를 한 번만 새로 작성해야합니다. 따라서 setter 및 getter를 호출하면 배열에 여러 번 추가 한 동일한 객체에 항상 영향을 미치게됩니다.

새 항목 집합을 추가 할 때마다 새 컨테이너 (ParsedExampleDataSet)를 새로 고침으로써이 문제를 수정할 수 있습니다. 다른

(localName.equals ("결과")) {

this.in_result = true; 
    myParsedExampleDataSet = new ParsedExampleDataSet(); 

}

또한

SAXParser를 문자를 호출 할 수있는 기억 (..) 여러 경우 필드에있는 내용에 따라 필드 당 시간. 따라서 캐릭터에서 얻은 값을 이미 해당 필드에 연결 한 값과 연결하는 것이 좋습니다.

(this.in_title) {

String value = myParsedExampleDataSet.getExtractedTitle(); 
value += new String(ch, start, length) 
myParsedExampleDataSet.setExtractedTitle(value); 

가}

+0

이 나를 위해 완벽하게 작동하는 경우! Tahnk 대단히! 솔직히 감사의 표현 방법을 모르겠습니다! 나는이 사이트에서 새롭고 매우 유용하다는 것을 알았다! :) –