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

     XmlPullParser 的使用

    生成xml文件的时候如果要出现“<”括号,可以使用脚本把被转成&lt;的<,替换回<  (不要直接使用路径,通过方法获取路径)

    sed -i 's/&lt;/</g' /storage/external_storage/sda1/123456.xml

    前提是sed命令要在这个Android系统中有效

  • 相关阅读:
    JS基础的入口函数以及函数
    JS基础的数组
    JS基础的数学函数、日期时间函数、运算符和流程语句
    JS基础的引入、注释、变量、数据类型和输出语法
    CSS3的动画、弹性盒子和多媒体查询
    CSS3的颜色(RGBA)透明度、渐变颜色、图片、旋转和过渡
    CSS3边框、背景和字体
    java基础(九)---继续聊聊map
    java基础(八)---map
    java基础(七)--- set
  • 原文地址:https://www.cnblogs.com/lipeineng/p/5856431.html
Copyright © 2020-2023  润新知