android 如何解析XML

来源:互联网 发布:数据结构和算法的关系 编辑:程序博客网 时间:2024/05/18 03:38

解析网络中的XML

在android中解析XML的方法有很多种,其中用的最多的是XmlPullParser 专门解析XML格式!

直接上代码吧,很容易明白的

新建一个NewsXmlUtil.java 工具类

解析的步骤:
1 创建XML解析器
XmlPullParser parser = Xml.newPullParser();
2 设置parser的解析参数
parser.setInput(in, “utf-8”);
3 获取到解析的事件类型
int type = parser.getEventType();
4 type != XmlPullParser.END_DOCUMENT如果 type 不等于最有的标签就一直循环读数据
其中channel 是model对象

package com.itheima.news;public class Channel {    private String title;    private String description;    private String imgurl;    private String type;    private String comment;    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    public String getImgurl() {        return imgurl;    }    public void setImgurl(String imgurl) {        this.imgurl = imgurl;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public String getComment() {        return comment;    }    public void setComment(String comment) {        this.comment = comment;    }}

工具类 传入的数据是InputStream

public class NewsXmlUtil {    /**     * 解析服务器返回的数据     *      * @param in     * @return     */    public static List<Channel> parserXml(InputStream in) {        try {            List<Channel> newsLists = null;            Channel channel = null;            // 1 创建xml的解析器            XmlPullParser parser = Xml.newPullParser();            // 2 设置parser的解析参数            parser.setInput(in, "utf-8");            // 3 获取到解析的事件类型            int type = parser.getEventType();            while (type != XmlPullParser.END_DOCUMENT) {                switch (type) {                case XmlPullParser.START_TAG:                    if ("channel".equals(parser.getName())) {                        // 解析到开始标签的时候 我要初始化一个list集合                        newsLists = new ArrayList<Channel>();                    } else if ("item".equals(parser.getName())) {                        // 把channel对象初始化                        channel = new Channel();                    } else if ("title".equals(parser.getName())) {                        // 设置title内容                        channel.setTitle(parser.nextText());                    } else if ("description".equals(parser.getName())) {                        // 设置描述的内容                        channel.setDescription(parser.nextText());                    } else if ("image".equals(parser.getName())) {                        // 设置image url                        channel.setImgurl(parser.nextText());                    } else if ("type".equals(parser.getName())) {                        // 设置新闻的类型                        channel.setType(parser.nextText());                    } else if ("comment".equals(parser.getName())) {                        // 设置                        channel.setComment(parser.nextText());                    }                    break;                case XmlPullParser.END_TAG:                    if ("item".equals(parser.getName())) {                        // 把channel对象加入到list中                        newsLists.add(channel);                        channel = null;                    }                    break;                default:                    break;                }                type = parser.next();// 不断的解析下一个事件            }            return newsLists;        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();            return null;        }    }}

工具类返回的是list数组!这样就可以解析XML了。
OK 就到这里吧

更多关于android的内容

0 0
原创粉丝点击