您可以使用 defaulthandler 来完成这项工作并为它编写相同的代码
boolean subelement,item,valid;rootvalid;
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if(localName.equal("subelement"))
subelement=true;
if(localName.equal("item"))
item=true;
if(subelement && localName.equal("valid"))
valid=true;
else if(localName.equal("valid"))
rootvalid=true;
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
{
if(localName.equal("subelement"))
subelement=false;
if(localName.equal("item"))
item=false;
if(subelement && localName.equal("valid"))
valid=false;
else if(localName.equal("valid"))
rootvalid=false;
}
public void characters(char ch[], int start, int length)
{
if(item)
//add item to arraylist
if(valid)
//add valid to arraylist
if(validroot)
//save valid of root}