2012-04-08 94 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 { 

}