0

使用 saxParser,我如何判断 endElement 何时完成?不幸的是,我有一个相当大的 XML 文件,需要将所有数据存储到 ArrayList 中,然后再将其传递给 InsertHelper 实用程序。如何测试 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 回答