0

saxParserを使用して、endElementが完了したことをどのように確認できますか?残念ながら、私はかなり大きなXMLファイルを持っており、InsertHelperユーティリティに渡す前にすべてのデータをArrayListに格納する必要があります。saxParserがループを完了したかどうかをテストするにはどうすればよいですか?

/**
 * Called when tag opening ( ex:- <name>AndroidPeople</name> -- <name> )
 */
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    currentElement = true;

    if (localName.equals("StoreDetails")) {
        //Log.i("TAG", "Store Details");
        newKOPStoreVersion = attributes.getValue("stores_version");
        //if (!dBHelper.getStoreXMLVersion().equals(newKOPStoreVersion)) { BUT BACK
        Log.i("TAG", "Store Details Changed");
        isStoreDetailsVersionChanged = true;
        //dBHelper.deleteStoreDetail();
        //currentValue = "";
        //buffer = new StringBuffer();
        dBHelper.setStoreXMLVersion(newKOPStoreVersion);
        /*}
        else BUT PACK
        {
            Log.i("TAG", "no change for KOP store version");
            throw new KOPSAXTerminatorException();
        }*/
    }

}

/**
 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
 */
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    currentElement = false;     

    //if (isStoreDetailsVersionChanged == true) { PUT BACK
    if (localName.equals("StoreID")) {
        buffer.toString().trim();
        storeDetails.setStoreId(buffer.toString());
    } else if (localName.equals("StoreName")) {
        buffer.toString().trim();
        storeDetails.setStoreName(buffer.toString());
    } else if (localName.equals("StoreDescription")) {
        buffer.toString().trim();
        storeDetails.setStoreDescription(buffer.toString());
    } else if (localName.equals("Location")) {
        buffer.toString().trim();
        storeDetails.setLocation(buffer.toString());
    } else if (localName.equals("Phonenumber")) {
        buffer.toString().trim();
        storeDetails.setPhoneNumber(buffer.toString());
    } else if (localName.equals("VisualmapID")) {
        buffer.toString().trim();
        storeDetails.setVisualMapId(buffer.toString());
    } else if (localName.equals("x")) {
        buffer.toString().trim();
        storeDetails.setCartX(buffer.toString());
    } else if (localName.equals("y")) {
        buffer.toString().trim();
        storeDetails.setCartY(buffer.toString());
    } else if (localName.equals("ClosestParkingLot")) {
        buffer.toString().trim();
        storeDetails.setClosestParkingLot(buffer.toString());
    } else if (localName.equals("RelatedCategory")) {
        buffer.toString().trim();
        storeDetails.setRelatedCategory(buffer.toString());
        //add buffer to arraylist - then loop array do the ih dance
        dataList.add(storeDetails);
//this is my arraylist and I'm attempting to loop over it outside of this class, but it seems to only contain the last value of the xml, not all the values.

    }

    buffer = new StringBuffer();

    //}
    if (localName.equals("StoreDetails")) {
        //Log.i("TAG","End StoreDetails");
        isStoreDetailsVersionChanged = false;
        //throw new KOPSAXTerminatorException();
    }
}

/**
 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
 * AndroidPeople Character )
 */
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    if (currentElement) {
        buffer.append(ch, start, length);
        currentElement = false;
    }
}

あなたがそれを見て喜んでいるなら、私はあなたに完全なコードを送ることができます。

4

1 に答える 1

1

SAXパーサーが終了すると、終了ドキュメントが呼び出されます。あなたがしなければならないのはあなたのハンドラー(要素メソッドを持っている同じクラス)のメソッドをオーバーライドすることです。

 @Override
 public void endDocument() throws SAXException {

 }
于 2012-04-08T03:07:50.667 に答える