Android中怎么解析XML文件和生成XML文件-创新互联

Android中怎么解析XML文件和生成XML文件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

靖远网站建设公司创新互联,靖远网站设计制作,有大型网站制作公司丰富经验。已为靖远上千多家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的靖远做网站的公司定做!

解析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();     }

关于Android中怎么解析XML文件和生成XML文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网页名称:Android中怎么解析XML文件和生成XML文件-创新互联
文章网址:http://myzitong.com/article/dscjgo.html