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>
原文:http://www.cnblogs.com/xiumukediao/p/4382133.html