码迷,mamicode.com
首页 > 其他好文 > 详细

XML读取

时间:2015-10-23 18:48:43      阅读:132      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!