Xml文件的解析与写入

作者: 人生苦短啊 | 来源:发表于2017-11-10 20:29 被阅读0次

前言

表白小可爱~~ 还没毕业的我,刚开始打算去大连华信实习,去了一天之后发现那里android部门并不招人,经过一番周折,在家人反对之下,铁了心出去找自己最喜欢的android公司,我认准了的事谁劝也不行.
这是到新公司实习的第一个项目,这里整理一下所学的知识点....
github地址:https://github.com/jjxs/SignageSetting.git

xml文件

<?xml version='1.0' encoding='utf-8'?>
<properties>
<comment>signage_application_setting</comment>
<entry key="server_url">啦啦啦</entry>
<entry key="application_root">手机手机号</entry>
<entry key="usb_root">gaga</entry><entry key="interval_get_schedule">这几句话</entry>
<entry key="interval_notify_status">自己家</entry>
<entry key="margin_next_schedule_download">自己家</entry>
</properties>

Xml文件的解析

androidxml的解析分为三种pull,sax,dom,在一番调查下,发现解析xml文件好用的是dom方法,这里我用dom方法解析
上代码

/**
     * 读取xml文件
     *
     * @param xmlfile 文件输入流
     * @return 集合对象
     */
    public Properties getProperties(File xmlfile) {
        Properties properties = new Properties();
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlfile);
            //获取根节点
            Element root = document.getDocumentElement();
              //直接获取我们需要的节点
            NodeList node = root.getElementsByTagName("entry");
             //遍历我们需要的节点得到我们要的数据
            for (int i = 0; i < node.getLength(); i++) {
                if (null == node.item(0).getFirstChild()) {
                    properties.setServer_url("");
                } else {
                    properties.setServer_url(node.item(0).getFirstChild().getNodeValue());
                }

                if (null == node.item(1).getFirstChild()) {
                    properties.setApplication_root("");
                } else {
                    properties.setApplication_root(node.item(1).getFirstChild().getNodeValue());
                }

                if (null == node.item(2).getFirstChild()) {
                    properties.setUsb_root("");
                } else {
                    properties.setUsb_root(node.item(2).getFirstChild().getNodeValue());
                }

                if (null == node.item(3).getFirstChild()) {
                    properties.setGet_schedule("");
                } else {
                    properties.setGet_schedule(node.item(3).getFirstChild().getNodeValue());
                }

                if (null == node.item(4).getFirstChild()) {
                    properties.setNotify_status("");
                } else {
                    properties.setNotify_status(node.item(4).getFirstChild().getNodeValue());
                }

                if (null == node.item(5).getFirstChild()) {
                    properties.setSchedule_download("");
                } else {
                    properties.setSchedule_download(node.item(5).getFirstChild().getNodeValue());
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

MainActivity里的代码

 //获取文件夹路径
        File dirFile = new File(Environment.getExternalStorageDirectory().toString() + "/signage");
        File xmlFile = new File(dirFile.getPath() + "/" + fileName);
        //获取xml中数据
        properties = pullService.getProperties(xmlFile);

写入xml文件

这里用了pull方法,写入xml文件

/**
     * 写入xml文件
     */
    public void writeXML(Properties properties, Context context) {

        File dirFile = new File(Environment.getExternalStorageDirectory().toString() + "/signage");
        if (!dirFile.exists() && !dirFile.isDirectory()) {
            dirFile.mkdir();
        }
        try {
            File xmlFile = new File(dirFile.getPath() + "/" + fileName);
            if (!xmlFile.exists()) {
                xmlFile.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(xmlFile);
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "utf-8");
            //设置文件头
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "properties");

            serializer.startTag(null, "comment");
            serializer.text("signage_application_setting");
            serializer.endTag(null, "comment");
            //server_url
            serializer.startTag(null, "entry");
            serializer.attribute(null, "key", "server_url");
            serializer.text(properties.getServer_url());
            serializer.endTag(null, "entry");
            //application_root
            serializer.startTag(null, "entry");
            serializer.attribute(null, "key", "application_root");
            serializer.text(properties.getApplication_root());
            serializer.endTag(null, "entry");
            //usb_root
            serializer.startTag(null, "entry");
            serializer.attribute(null, "key", "usb_root");
            serializer.text(properties.getUsb_root());
            serializer.endTag(null, "entry");
            //interval_get_schedule
            serializer.startTag(null, "entry");
            serializer.attribute(null, "key", "interval_get_schedule");
            serializer.text(properties.getGet_schedule());
            serializer.endTag(null, "entry");
            //interval_notify_status
            serializer.startTag(null, "entry");
            serializer.attribute(null, "key", "interval_notify_status");
            serializer.text(properties.getNotify_status());
            serializer.endTag(null, "entry");
            //margin_next_schedule_download
            serializer.startTag(null, "entry");
            serializer.attribute(null, "key", "margin_next_schedule_download");
            serializer.text(properties.getSchedule_download());
            serializer.endTag(null, "entry");

            serializer.endTag(null, "properties");
            serializer.endDocument();
            fos.close();
            Toast.makeText(context, "保存文件成功", Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

涉及到的UI问题

点击按钮使输入框失去焦点

btn_apply.requestFocus();
btn_apply.requestFocusFromTouch();

这里让按钮控件获取焦点就等于让editText失去焦点

关闭输入框

edit_IntervalToGetSchedule.setFocusable(false);
edit_IntervalToGetSchedule.setEnabled(false);

对输入框文本监听

new TextWatcher()

相关文章

网友评论

    本文标题:Xml文件的解析与写入

    本文链接:https://www.haomeiwen.com/subject/hasbmxtx.html