加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

解析中国国家气象局天气预报信息接口 xml文件,包含省市县三层结

发布时间:2020-12-16 08:50:39 所属栏目:百科 来源:网络整理
导读:最近在做一个基于天气预报的app,解析的是xml文件格式如下。 province id = "01" name = "北京" city id = "0101" name = "北京" county id = "010101" name = "北京" weatherCode= "101010100" / county id = "010102" name = "海淀" weatherCode= "1010102

最近在做一个基于天气预报的app,解析的是xml文件格式如下。

<province id="01" name="北京">
    <city id="0101" name="北京">
      <county id="010101" name="北京" weatherCode="101010100"/>
      <county id="010102" name="海淀" weatherCode="101010200"/>
      <county id="010103" name="朝阳" weatherCode="101010300"/>
      <county id="010104" name="顺义" weatherCode="101010400"/>
      <county id="010105" name="怀柔" weatherCode="101010500"/>
      <county id="010106" name="通州" weatherCode="101010600"/>
      <county id="010107" name="昌平" weatherCode="101010700"/>
      <county id="010108" name="延庆" weatherCode="101010800"/>
      <county id="010109" name="丰台" weatherCode="101010900"/>
      <county id="010110" name="石景山" weatherCode="101011000"/>
      <county id="010111" name="大兴" weatherCode="101011100"/>
      <county id="010112" name="房山" weatherCode="101011200"/>
      <county id="010113" name="密云" weatherCode="101011300"/>
      <county id="010114" name="门头沟" weatherCode="101011400"/>
      <county id="010115" name="平谷" weatherCode="101011500"/>
    </city>
  </province>

源文件如下:

public class ParseCity {
    public static void parse(InputStream in,CoolWeatherDB coolWeatherDB) {
        try{
            //init
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(in,"utf-8");//set stream
            int eventType = xmlPullParser.getEventType();
            //获得的是事件类型,常用的有:XmlPullParser.END_DOCUMENT(xml文档结束)
            //XmlPullParser.START_DOCUMENT(文档开始),XmlPullParser.START_TAG(开始标签)
            //XmlPullParser.END_TAG(结束标签).XmlPullParser.TEXT(内容)
            String cityid = "";
            String fid = "";
            //init
            while(eventType != XmlPullParser.END_DOCUMENT) {
                String nodeName = xmlPullParser.getName();
                switch(eventType) {
                    //begin to parse somenode
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG: {
                    String tagName = xmlPullParser.getName();//get the tag name,China,province,city,county
                        if("province".equals(tagName)) {
                             int count = xmlPullParser.getAttributeCount();//get the count of province,1
                             String provinceId = null;
                             String provinceName = null;
                             for (int i = 0; i < count; i++) {
                                    String attrName = xmlPullParser.getAttributeName(i);//get id,get name
                                    String attrValue = xmlPullParser.getAttributeValue(i);//get 01,get 北京
                                    if ("id".equals(attrName))
                                    {
                                       fid = attrValue;//set foreign id in city object 
                                       provinceId = attrValue;
                                    }
                                    else if("name".equals(attrName)) {
                                        provinceName = attrValue;
                                    }
                                }
                                Province province = new Province();
                                province.setProvinceCode(provinceId);
                                province.setProvinceName(provinceName);
                                //put the parsed data into the table Province
                                coolWeatherDB.saveProvince(province);
                        }
                        if("city".equals(tagName)) {
                             int count = xmlPullParser.getAttributeCount();
                              String cityName = null;
                              String cityCode = null;
                              int provinceId = Integer.parseInt(fid);
                             for (int i = 0; i < count; i++) {
                                    String attrName = xmlPullParser.getAttributeName(i);
                                    String attrValue = xmlPullParser.getAttributeValue(i);
                                    if ("id".equals(attrName)) {
                                       cityid = attrValue;//set foreign id in county object
                                       cityCode  = attrValue;
                                    }
                                    else if("name".equals(attrName)) {
                                        cityName = attrValue;
                                    }
                                }
                                City city = new City();
                                city.setCityCode(cityCode);
                                city.setCityName(cityName);
                                city.setProvinceId(provinceId);
                                coolWeatherDB.saveCity(city);
                        }
                        if("county".equals(nodeName)) {
                            int count = xmlPullParser.getAttributeCount();
                            String countyName = null;
                            String countyCode = null;
                            int cityId = Integer.parseInt(cityid);
                             for (int i = 0; i < count; i++) {
                                    String attrName = xmlPullParser.getAttributeName(i);
                                    String attrValue = xmlPullParser.getAttributeValue(i);
                                    if ("id".equals(attrName)) {
                                    }
                                    else if("name".equals(attrName)) {
                                        countyName = attrValue;
                                    }else if("weatherCode".equals(attrName)) {
                                        countyCode = attrValue;
                                    }
                                }
                             County county = new County();
                                county.setCountyCode(countyCode);
                                county.setCountyName(countyName);
                                county.setCityId(cityId);
                                coolWeatherDB.saveCounty(county);
                        }
                        break;
                    }
                case XmlPullParser.END_TAG:{
                    break;
                }
                default:
                    break;
                }
                 eventType = xmlPullParser.next();
                }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读