使用此示例代码
public class SAXParserDemo extends DefaultHandler {
private String currentTag = "";
private StringBuffer responseTag;
private String str;
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
currentTag = localName;
if ("tag1".equalsIgnoreCase(localName)) {
responseTag = new StringBuffer();
}else if ("tag2".equalsIgnoreCase(localName)) {
responseTag = new StringBuffer();
}else if ("div".equalsIgnoreCase(localName)) {
responseTag = new StringBuffer();
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
super.endElement(uri, localName, name);
String responseValue = responseTag.toString().trim();
if ("tag1".equalsIgnoreCase(localName)) {
Log.v("TAG", "Tag 1 value "+responseValue);
}else if ("tag2".equalsIgnoreCase(localName)) {
Log.v("TAG", "Tag 2 value "+responseValue);
}else if ("div".equalsIgnoreCase(localName)) {
Log.v("TAG", "div value "+responseValue);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
str = new String(ch, start, length);
if ("tag1".equalsIgnoreCase(currentTag)) {
responseTag.append(str);
}else if ("tag2".equalsIgnoreCase(currentTag)) {
responseTag.append(str);
}else if ("div".equalsIgnoreCase(currentTag)) {
responseTag.append(str);
}
}
}