解析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 的使用

更多相关文章

  1. 具体介绍使用XmlReader读取xml文件的代码案例
  2. xml,文件操作功能类的示例代码详解
  3. XML布局文件的代码案例分享
  4. 详解读写XML文件的代码案例
  5. XML开发基础-查看XML文件
  6. xml文件正确性验证类的示例代码分析
  7. 详解xmlhttp组件获取远程文件并筛选出目标数据
  8. xml文件如何编辑的详情介绍
  9. 教你如何打开xml文件的详解

随机推荐

  1. Android伸手党系列之二:Android开发基础知
  2. Android 底层学习札记
  3. Android 带图标的textview
  4. Android 多屏适配
  5. Android(安卓)-> 如何避免Handler引起内
  6. Android 融云SDK 集成
  7. 【边做项目边学Android】手机安全卫士05_
  8. Android(安卓)Launcher 之 图标加框 优化
  9. android 开机流程
  10. android arm debug