码迷,mamicode.com
首页 > 移动开发 > 详细

Android XML pull解析

时间:2015-03-31 23:39:31      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

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>

Android XML pull解析

标签:

原文地址:http://www.cnblogs.com/xiumukediao/p/4382133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!