Android 解析XML檔案和產生XML檔案,androidxml

來源:互聯網
上載者:User

Android 解析XML檔案和產生XML檔案,androidxml

解析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檔案

//預設是沒有換行的
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(); }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.