首页 > 移动平台 > 详细

Android XML pull解析

时间:2015-03-31 23:37:53      阅读:272      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!