首页 > 其他 > 详细

XML读取

时间:2015-10-23 18:47:53      阅读:241      评论:0      收藏:0      [点我收藏+]
    private void readXml() throws XmlPullParserException, IOException {
        XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
        parser.setInput(openFileInput("peoples.xml"), "utf-8");
        int type = parser.getEventType();
        ArrayList<People> arrayList = null;
        People p = null;
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
                case XmlPullParser.START_TAG:
                    if ("peoples".equals(parser.getName())) {
                        arrayList = new ArrayList<People>();
                    } else if ("people".equals(parser.getName())) {
                        p = new People();
                    } else if ("name".equals(parser.getName())) {
                        p.setName(parser.nextText());
                    } else if ("age".equals(parser.getName())) {
                        p.setAge(Integer.parseInt(parser.nextText()));
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("people".equals(parser.getName())) {
                        arrayList.add(p);
                        p = null;
                    }
                    break;
            }
            type = parser.next();
        }
        for (People pp : arrayList) {
            System.out.println(pp.getName() + " " + pp.getAge());
        }
    }

 

XML读取

原文:http://www.cnblogs.com/linson0116/p/4905135.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!