zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    如何将Python项目发布到PyPI
    hashlib的md5计算
    使用hexo和coding建立静态博客站点
    mysql 使用记录
    linux QA
    linux 使用记录
    转载-linux内核长什么样
    mysql 更改默认字符集
    Say goodbye
    SSH proxycommand 不在同一局域网的机器ssh直连
  • 原文地址:https://www.cnblogs.com/xiumukediao/p/4382133.html
Copyright © 2011-2022 走看看