詳細介紹Android 解析XML檔案和產生XML檔案的範例程式碼

黄舟
發布: 2017-03-20 16:42:11
原創
1894 人瀏覽過

解析XML

public static void initXML(Context context) {
        //can't create in /data/media/0 because permission 
        //can create in /sdcard/hotel
        File mSettings = new File(HOTEL_PATH_XML);
        if (!mSettings.exists()) {
            mSettings.mkdirs();
        }
        File settings = new File(mSettings,"settings.xml");
        Log.i("XmlPullParser-----settings", settings+"+1+");
        if (!settings.exists()) {
            try {
                Log.i("XmlPullParser-----settings", settings+"+2+");
                settings.createNewFile();
                initSettings(settings);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            return;
        }
         
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(new FileInputStream(settings), "utf-8");
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                Log.i("XmlPullParser-----TAG", eventType+"");
                if (eventType == XmlPullParser.START_TAG) {
                    String tag = xpp.getName();
                    Log.i("XmlPullParser-----TAG", "tag---------"+tag+"");
                    if (tag.equals("item")) {
                        String id = xpp.getAttributeValue(null, "id");
                        String value = xpp.getAttributeValue(null, "value");
                        if (id.equals("server")) {
                            sServerAddr = value;
                        } else if (id.equals("hotel")) {
                            sHid = value;
                        } else if (id.equals("room")) {
                            sRoomNum = value;
                        }
                    }
                }
                eventType = xpp.next();
            }
            Log.i("XmlPullParser-----TAG", eventType+"exist the xunhuan");
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
登入後複製

產生XML檔

//默认是没有换行的<br>public static void initSettings(final File settings) {
        new Thread(new Runnable() {
             
            @Override
            public void run() {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(settings);
                    XmlSerializer serializer = Xml.newSerializer();
                    serializer.setOutput(fos, "UTF-8");
                    serializer.startDocument("UTF-8", true);
                    serializer.startTag(null, "config");
                    serializer.startTag(null, "category");
                    serializer.attribute(null, "name", "hot");
                    // server
                    serializer.startTag(null, "item");
                    serializer.attribute(null, "id", "server");
                    serializer.attribute(null, "value", "");
                    serializer.endTag(null, "item");
                    // hid
                    serializer.startTag(null, "item");
                    serializer.attribute(null, "id", "hotel");
                    serializer.attribute(null, "value", "");
                    serializer.endTag(null, "item");
                    // room
                    serializer.startTag(null, "item");
                    serializer.attribute(null, "id", "room");
                    serializer.attribute(null, "value", "");
                    serializer.endTag(null, "item");
                     
                    serializer.endTag(null, "category");
                    serializer.endTag(null, "config");
                    serializer.endDocument();
                    serializer.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
登入後複製

XmlPullParser 的使用

以上是詳細介紹Android 解析XML檔案和產生XML檔案的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板