从xml文件中取数据

也是直接上代码

try {
            String path = getApplication().getFilesDir().toString();
            File file = new File(path, "data.xml");
            FileInputStream fis = new FileInputStream(file);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(fis, "utf-8");
            int type = parser.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                case XmlPullParser.START_TAG:
                    if ("sample".equals(parser.getName())) {
                        data = parser.nextText();  //data是一个string型的,用来保存从xml文件中读取出来的数据
                    }
                    break;

                case XmlPullParser.END_DOCUMENT:
                    break;
                }
                type = parser.next();
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

在if()的下面再接着读取你需要的数据

猜你喜欢

转载自blog.csdn.net/weixin_38752761/article/details/78397009