现在的位置: 首页 > 综合 > 正文

Android 读写xml文件

2014年06月03日 ⁄ 综合 ⁄ 共 3401字 ⁄ 字号 评论关闭

Android里面操作xml文件主要有三种方式:

SAX方式,DOM方式,PULL方式。据说因为DOM的方式比较耗内存,而手机最纠结的就是存储空间小的问题,所以在Android里面不提倡使用DOM的方式。而SAX方式和PULL方式的思想比较的相似,原理似乎是同出一则的,实现的思想好像是读取字符串的每一个单词一样,每读取一段就判断读取的是什么内容然后执行相应的方法,直至文件的结尾。

本文的例子主要采用PULL方式读写xml,废话不多说,直接上代码。

xml文件如下

<persons>
  <person>
    <name>北京</name>
    <number>123324324</number>
  </person>
  <person>
    <name>东坡</name>
    <number>1-591-992-5643</number>
  </person>
  <person>
    <name>俊伟</name>
    <number>1-379-784-7916</number>
  </person>
</persons>



读xml的代码:

public static boolean readFixedNumbersFromXml() {
        XmlPullParser xmlParser = Xml.newPullParser();
        File file = new File(LISTENED_NUMBERS_FILE);
        if (!file.exists()) {
            Log.d(TAG, "file :" + LISTENED_NUMBERS_FILE + " is not exist, create it!");
            return false;
        }

        int eventType = 0;
        try {
            FileInputStream fis = new FileInputStream(file);
            xmlParser.setInput(fis, "UTF-8");
            eventType = xmlParser.getEventType();

            ListenInfoManager.ListenedPerson person = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                Log.d(TAG, "eventType=" + eventType);
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        Log.d(TAG, "name=" + xmlParser.getName());
                        if ("person".equals(xmlParser.getName())) {
                            person = new ListenInfoManager.ListenedPerson();
                        }

                        if ("name".equals(xmlParser.getName())) {
                            if (person != null) {
                                person.setName(xmlParser.nextText());
                            }
                        }

                        if ("number".equals(xmlParser.getName())) {
                            if (person != null) {
                                person.setNumber(xmlParser.nextText());
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("person".equals(xmlParser.getName())) {
                            Log.d(TAG, "name:" + xmlParser.getName() + ", person=" + person);
                            ListenInfoManager.self().addToCustomNumberList(person);
                            person = null;
                        }
                        break;
                    default:
                        break;
                }
                eventType = xmlParser.next();
            }
        } catch (FileNotFoundException e) {
            Log.d(TAG, "file not found exception!");
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d(TAG, "XmlPullParserException!");
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "IOException!");
            e.printStackTrace();
        }

        return true;
    }


写xml代码如下:

public static boolean writeFixedNumbersToXmlFile() {
        FileOutputStream fos = null;
        File file = new File(LISTENED_NUMBERS_FILE_PATH);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            file = new File(LISTENED_NUMBERS_FILE);
            if (!file.createNewFile()) {
                Log.d(TAG, "file already exist!");
                file.delete();
                if (!file.createNewFile()) {
                    Log.d(TAG, "Create file failed!");
                    return false;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            Log.d(TAG, "create xml file failed!");
        }
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return false;
        }
        XmlSerializer serializer = Xml.newSerializer();
        try {
            serializer.setOutput(fos, "UTF-8");

            //缩进
            serializer.setFeature(
                    "http://xmlpull.org/v1/doc/features.html#indent-output",
                    true);
            serializer.startDocument("UTF-8", true);
            serializer.startTag(null, "persons");
            for (ListenedPerson person : ListenInfoManager.self().getCustomNumberList()) {
                serializer.startTag(null, "person");

                serializer.startTag(null, "name");
                serializer.text(person.getName());
                serializer.endTag(null, "name");

                serializer.startTag(null, "number");
                serializer.text(person.getNumber());
                serializer.endTag(null, "number");

                serializer.endTag(null, "person");
            }
            serializer.endTag(null, "persons");
            serializer.endDocument();

            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            Log.d(TAG, "IOException write xml file failed!");
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "Exception write xml file failed!");
            return false;
        }
        return true;
    }

抱歉!评论已关闭.