1、解析XML:使用Pull方式。
需要导入jar包:xmlpull-xpp3-1.1.4c.jar
//Pull解析XML文件
private void parseXMLWithPull(String xml_data) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xml_data));
int eventType=xmlPullParser.getEventType();
String id = "";
String name = "";
String version = "";
while (XmlPullParser.END_DOCUMENT != eventType) {
String nodeName = xmlPullParser.getName();
switch (eventType) {
//解析时的判断逻辑
case XmlPullParser.START_TAG:
if ("id".equals(nodeName)) {
id = xmlPullParser.nextText();
} else if ("name".equals(nodeName)) {
name = xmlPullParser.nextText();
} else if ("version".equals(nodeName)) {
version = xmlPullParser.nextText();
}
break;
//完成解析某个结点
case XmlPullParser.END_TAG:
if ("app".equals(nodeName)) {
Log.d("id is ",id);
Log.d(" name is ",name);
Log.d(" version is ",version);
}
break;
default:
break;
}
eventType = xmlPullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
————————————————
原文地址:https://www.cnblogs.com/ly570/p/11379421.html