下面代码只能在Android系统上运行,不能使用Test方式运行,更加推荐PULL的方式解析,简单方便,不用写那么多代码
1.PULL解析
public void pullXML() throws XmlPullParserException, IOException {
////创建解析工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//创建解析
XmlPullParser xmlPullParser = factory.newPullParser();
//要解析的对象
String in = "<apps>\n" +
"\t<app>\n" +
"\t\t<id>1</id>\n" +
"\t\t<version>1.0</version>\n" +
"\t\t<name>qq</name>\n" +
"\t</app>\n" +
"\t<app>\n" +
"\t\t<id>2</id>\n" +
"\t\t<version>2.0</version>\n" +
"\t\t<name>weixin</name>\n" +
"\t</app>\n" +
"</apps>";
//设置输入
xmlPullParser.setInput(new StringReader(in));
String id = "";
String version = "";
String name = "";
//启动解析
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
//获取本次得到的结点名
String tagName = xmlPullParser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
//开始某个结点
if ("id".equals(tagName)) {
//向前推动解析
id = xmlPullParser.nextText();
}
if ("version".equals(tagName)) {
//向前推动解析
version = xmlPullParser.nextText();
}
if ("name".equals(tagName)) {
//向前推动解析
name = xmlPullParser.nextText();
}
break;
case XmlPullParser.END_TAG:
//结束某个结点
if ("app".equals(tagName)) {
Log.d(TAG, "pullXML: id=" + id);
Log.d(TAG, "pullXML: version=" + version);
Log.d(TAG, "pullXML: name=" + name);
}
break;
default:
break;
}
//继续向前解析
eventType = xmlPullParser.next();
}
}
2.SAX解析
- 自定义ContentHandler,实现解析逻辑
public class MySAXXml extends DefaultHandler {
private static final String TAG = "MySAXXml";
private String tagName;
private StringBuilder id;
private StringBuilder version;
private StringBuilder name;
/**
* 文档开始
*
* @throws SAXException
*/
@Override
public void startDocument() throws SAXException {
super.startDocument();
// Log.d(TAG, "startDocument: ");
id = new StringBuilder();
version = new StringBuilder();
name = new StringBuilder();
}
/**
* 文档结束
*
* @throws SAXException
*/
@Override
public void endDocument() throws SAXException {
super.endDocument();
// Log.d(TAG, "endDocument: ");
}
/**
* 元素开始
*
* @param uri
* @param localName
* @param qName
* @param attributes
* @throws SAXException
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
// Log.d(TAG, "startElement: ");
//记录当前结点名
tagName = localName;
}
/**
* 元素结束
*
* @param uri
* @param localName
* @param qName
* @throws SAXException
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
// Log.d(TAG, "endElement: ");
if ("app".equals(localName)) {
Log.d(TAG, "endElement: id=" + id.toString());
Log.d(TAG, "endElement: version=" + version.toString());
Log.d(TAG, "endElement: name=" + name.toString());
//清空
id.setLength(0);
version.setLength(0);
name.setLength(0);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
// Log.d(TAG, "characters: ");
if ("id".equals(tagName)) {
id.append(ch, start, length);
} else if ("version".equals(tagName)) {
version.append(ch, start, length);
} else if ("name".equals(tagName)) {
name.append(ch, start, length);
}
}
}
- 测试
/**
* sax方式解析
*/
private void saxXML() throws ParserConfigurationException, SAXException, IOException {
//创建SAX解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//得到XMLReader
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
//设置自定义的Handler
xmlReader.setContentHandler(new MySAXXml());
//要解析的对象
String in = "<apps>\n" +
"\t<app>\n" +
"\t\t<id>1</id>\n" +
"\t\t<version>1.0</version>\n" +
"\t\t<name>qq</name>\n" +
"\t</app>\n" +
"\t<app>\n" +
"\t\t<id>2</id>\n" +
"\t\t<version>2.0</version>\n" +
"\t\t<name>weixin</name>\n" +
"\t</app>\n" +
"</apps>";
//设置解析资源并执行解析
xmlReader.parse(new InputSource(new StringReader(in)));
}