关闭

PullUtils解析xml

124人阅读 评论(0) 收藏 举报
public class PullUtils {
    private List<NewsBean> list;
    private NewsBean n;

    public  List<NewsBean> getlist(String result){
        XmlPullParser parser = Xml.newPullParser();
        if(list==null){
            list = new ArrayList<NewsBean>();
        }else{
            list.clear();
        }
//把字符串转换成流
        ByteArrayInputStream is = new  ByteArrayInputStream(result.getBytes());
        try {
            parser.setInput(is, "utf-8");
            int type = parser.getEventType();
            while(type!=parser.END_DOCUMENT){
                String name = parser.getName();
                switch (type) {
                case XmlPullParser.START_TAG:
                    if("news".equals(name)){
                        n = new NewsBean();
                    }else if("id".equals(name)){
                        n.setId(parser.nextText());
                    }else if("title".equals(name)){
                        n.setTitle(parser.nextText());
                    }else if("body".equals(name)){
                        n.setBody(parser.nextText());
                    }else if("commentCount".equals(name)){
                        n.setCommentCount(parser.nextText());
                    }else if("author".equals(name)){
                        n.setAuthor(parser.nextText());
                    }else if("authorid".equals(name)){
                        n.setAuthorid(parser.nextText());
                    }else if("pubDate".equals(name)){
                        n.setPubDate(parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if("news".equals(name)){
                        list.add(n);
                    }
                    break;

                }
                
                type=parser.next();
            }
            
            
            return list;
            
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        };
        return null;
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7601次
    • 积分:270
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论