2012-04-08 50 views
0

使用saxParser,我怎麼知道endElement何時完成?不幸的是,我有一個相當大的XML文件,需要將所有數據存儲到ArrayList中,然後再將它傳遞給InsertHelper實用程序。如何測試saxParser是否已完成循環?Android + saxParser endElement完成循環?

/** 
* 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; 
    } 
} 

如果您願意查看,我可以向您發送完整的代碼。

+0

theres an enddocument method,you can override – L7ColWinters 2012-04-08 03:05:53

回答

1

當SAX解析器完成時,會調用最終文檔。你所要做的就是重寫你的處理程序中的方法(具有元素方法的同一個類)。

@Override 
public void endDocument() throws SAXException { 

}