将string改为stringbuffer接收数据。
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.content.res.XmlResourceParser;
public class SAXParserXML {
private StringBuffer currentValue = new StringBuffer();
private Map<String,Object> map=new HashMap<String,Object>();
public void parserXML(String secretInfo){
try{
SAXParserFactory factory=SAXParserFactory.newInstance();
XMLReader reader=factory.newSAXParser().getXMLReader();
reader.setContentHandler(new parserXMLClass());
reader.parse(new InputSource(new StringReader(secretInfo)));
}catch(Exception ex){
ex.printStackTrace();
}
}
class parserXMLClass extends DefaultHandler{
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue.append(ch, start, length);
}
@Override
public void endDocument() throws SAXException {
System.out.println("***endDocument***");
}
@Override
public void startDocument() throws SAXException {
currentValue.delete(0, currentValue.length());
System.out.println("***startDocument***");
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
map.put(qName, currentValue.toString());
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(localName.equals("infomode")){
for(int i=0;i<attributes.getLength();i++)
map.put(attributes.getLocalName(i), attributes.getValue(i));
}
}
}
public Map<String, Object> getMap() {
System.out.println("解析的数据是:"+map);
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}