解析获得的网页数据(XML文件或JSON文件)

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();
}
}
————————————————

posted on 2019-08-19 20:22  激流勇进1  阅读(654)  评论(0编辑  收藏  举报