2011-07-27 6 views
0

私はExampleHandler extends from DefaultHandlerという名前のクラスを持っています。Android Xml Handlerは、xmlファイルの最後のオブジェクトのみを保持します

このクラスは、(Web上)外部のXMLファイルからデータを読み込み、このxmlファイルの構造は次のとおりです。

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

私は、このxmlファイルと同じ構造を持つクラスを作成しましたが(ParsedExampleDataSetと呼ばれますそれはゲッターとセッターを持っています)。

すべての 'result'オブジェクトを含む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によくある誤解です。 javaはポインタ以外のすべてのプリミティブをポインタとして扱うことを覚えておく必要があります。あなたのケースでは、データコンテナオブジェクトを1回だけ新しくしています。したがって、セッターとゲッターを呼び出すと、配列に複数回追加する同じオブジェクトに常に影響します。

新しい項目セットを追加するたびに、新しいコンテナ(ParsedExampleDataSet)を新しくすることで、この問題を解決できます。他

(localName.equals( "結果")){

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

}

また

SAXParserの文字を呼び出すことができることを覚えておいてください(..)複数の場合現場に何があるかに応じて1フィールドあたりの回数。だから、あなたが文字から得た値をあなたがすでにそのフィールドに持っているものに連結したいと思うかもしれません。

これは私のために完璧に動作します(this.in_title){

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

}

+0

場合!!! Tahnkあなたはとても大変です! 正直、どうもありがとうございます! 私はこのサイトでは新しく、とても役に立ちます! :) –

関連する問題