Android XML pull解析
List<weatherinfos> listinfo = null;
weatherinfos singleinfo = null;
XmlPullParser parse = Xml.newPullParser();
try {
parse.setInput(in, "utf-8");
int eventtype = parse.getEventType();
while (eventtype != XmlPullParser.END_DOCUMENT) {
switch (eventtype) {
case XmlPullParser.START_DOCUMENT:
listinfo = new ArrayList<weatherinfos>();
break;
case XmlPullParser.START_TAG:
if (parse.getName().equals("city")) {
singleinfo = new weatherinfos();
String strid = parse.getAttributeValue(0);
singleinfo.setId(Integer.parseInt(strid));
} else if (parse.getName().equals("temp")) {
// eventtype = parse.next();
String strtemp = parse.nextText();
singleinfo.setTemp(strtemp);
} else if (parse.getName().equals("weather")) {
// eventtype = parse.next();
String strweather = parse.nextText();
singleinfo.setWeather(strweather);
} else if (parse.getName().equals("wind")) {
// eventtype = parse.next();
String stewind = parse.nextText();
singleinfo.setWind(stewind);
} else if (parse.getName().equals("name")) {
// eventtype = parse.next();
String cityname = parse.nextText();
singleinfo.setName(cityname);
} else if (parse.getName().equals("pm")) {
// eventtype = parse.next();
String strpm = parse.nextText();
singleinfo.setPm(strpm);
} break;
case XmlPullParser.END_TAG:
if (parse.getName().equals("city")) {
listinfo.add(singleinfo);
singleinfo = null;
} break;
default: break;
}
eventtype = parse.next();
}
} catch (Exception e) {
// TODO: handle exception
}
解析如此XML
<?xml version = "1.0" encoding = "utf-8"?>
<infos>
<city id = "1">
<temp>20/30</temp>
<weather>阴雨</weather>
<wind>南风</wind>
<name>上海</name>
<pm>200</pm>
</city>
<city id = "2">
<temp>20/33</temp>
<weather>雨</weather>
<wind>北风</wind>
<name>南京</name>
<pm>300</pm>
</city>
</infos>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。