public List<News> getListFromXml(InputStream in) {
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(in, "UTF-8");//设置你要解析那个文件,使用什么编码格式把它读入到内存
            int eventType = parser.getEventType();
            String name = null;
            News news = null;
            while (parser.END_DOCUMENT != eventType) {
                name = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if ("news".equals(name)) {
                        List<News> list = new ArrayList<News>();
                    } else if ("new".equals(name)) {
                        news = new News(); //当遇到了new节点的时候创建news对象并解析id属性给news对象

                    } else if ("title".equals(name)) {
                        news.setTitle(parser.nextText());
                    } else if ("detail".equals(name)) {
                        news.setDetail(parser.nextText());
                    } else if ("comment".equals(name)) {
                        news.setComment(Integer.valueOf(parser.nextText()));
                    } else if ("image".equals(name)) {
                        news.setImageuri(parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("new".equals(name)) {
                        list.add(news);
                    }
                default:
                    break;
                }
                eventType = parser.next();

            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;

    }

 

posted on 2015-11-03 16:48  0代码狂人0  阅读(716)  评论(0编辑  收藏  举报