试试这个
public class MyXmlHandler extends DefaultHandler{
boolean isCategory, isChildCategory,isSubCategory = false;
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("Category"))
{
isCategory = true;
String id = attributes.getValue("id");
}else if(qName.equalsIgnoreCase("SubCategory"){
isSubCategory =true;
String id = attributes.getValue("id");
}else if(qName.equalsIgnoreCase("ChildCategory")){
// do something
String id = attributes.getValue("id");
isChildCategory =false;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equalsIgnoreCase("Category ")){
isCategory = false;
}if(qName.equalsIgnoreCase("subCategory ")){
isSubCategory = false;
}
if(qName.equalsIgnoreCase("ChildCategory ")){
isChildCategory =false;
}
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
*/
public void characters(char[] ch, int start, int length)
throws SAXException {
String value = new String(ch,start,length);
if(isCategory)
{
String str = value;
}
if(isSubCategory)
{
String str1 = value;
}
}
}
////////
//TO call MyXmlHandler
try{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
MyXmlHandler datahandler=new MyXmlHandler();
xr.setContentHandler(datahandler);
xr.parse(new InputSource("url"));
}catch(Exception e)
{
Log.e("SAX XML", "i solve error");
}