温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android中xml的pull解析

发布时间:2020-08-16 17:46:49 来源:网络 阅读:360 作者:杨少华_55161 栏目:移动开发

xml的pull解析:


   //类加载器加载xml文件

InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml");

    //生成xml的pull解析器

    XmlPullParser pull = Xml.newPullParser();

    try {

    //设置输入流

pull.setInput(is, "utf-8");

        //解析器当前处于的状态

int type = pull.getEventType();

Weather weather = null ;

while (type != XmlPullParser.END_DOCUMENT) {

switch (type) {

            //当前是开始标签的节点

    case XmlPullParser.START_TAG:

                //拿到节点标签的名字

        String name = pull.getName();

            //判断节点标签的名字 (下面同这儿),然后放到weather对象中  

        if("channel".equals(name)){

            weather = new Weather();

            weather.setId(pull.getAttributeValue(0));

        }

            

        if("city".equals(name)){

            weather.setName(pull.nextText());

        }

        if("temp".equals(name)){

            weather.setTemp(pull.nextText());

        }

        if("wind".equals(name)){

            weather.setWind(pull.nextText());

        }

        break;

        //如果是结束标签的节点,此处将weather对象放入list集合保存

case XmlPullParser.END_TAG:

    String tagname = pull.getName();

    if("channel".equals(tagname)){

        list.add(weather);

    }

default:

    break;

        }

//此处必须写,否则不会向下执行

        type = pull.next();

        }

    } catch (Exception e) {

e.printStackTrace();

    }

}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI