dom4j解析xml

1、Android studio导入Dom4j,implementation 'org.dom4j:dom4j:2.1.1'
2、列子如下,注意红色部分:   


public static void saveInstallOrDevInfoState(
            int installState,
            int devInfoState,
            int usbModeState
    ) {

        File xmlFile = new File(PathConstants.INSTALL_STATE_PATH);
        if(!xmlFile.exists()){
            creatXmlFile();
        }

        updateXmlItem("install",String.valueOf("1"));
        updateXmlItem("devinfo",String.valueOf(devInfoState));
        updateXmlItem("usbmode",String.valueOf(usbModeState));


    }



    public static boolean insertXmlItem(String element,String text){
        SAXReader reader=new SAXReader();
        Document dom;
        FileWriter writer= null;

        try {
            dom = reader.read(new File(PathConstants.INSTALL_STATE_PATH));
            Element root=dom.getRootElement();

            //文档对象创建一个元素
            Element ele = DocumentHelper.createElement(element);
            //设置元素特价的标签体
            ele.setText(text);
            //将内存中的dom书写入xml文档中,从而使xml中的数据更新
            root.add(ele);
            writer=new FileWriter(PathConstants.INSTALL_STATE_PATH);
            dom.write(writer);
            writer.flush();

        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            if(writer !=null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;

    }

    public static boolean updateXmlItem(String element,String text){
        SAXReader reader=new SAXReader();
        Document dom= null;
        boolean ret =true;
        try {
            dom = reader.read(new File(PathConstants.INSTALL_STATE_PATH));
            Element root=dom.getRootElement();


            Element ele = root.element(element);
            if(ele == null){
                ret = insertXmlItem(element,text);
            }else{
                Log.e(TAG,ele.getText());
                ele.setText(text);
            }
            writer(dom,PathConstants.INSTALL_STATE_PATH);
            Log.e(TAG,ele.getText());
        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        }
        return ret;
    }

    public static boolean isExist(String element) {
        SAXReader reader=new SAXReader();


        Document dom= null;
        try {
            dom = reader.read(new File(PathConstants.INSTALL_STATE_PATH));
            Element root=dom.getRootElement();


            Element ele = root.element(element);
            if(ele == null){
                return false;
            }
            Log.e(TAG,ele.element(element).getText());
        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        }

        return true;

    }

    public static void writer(Document document,String path) {


        FileWriter out = null;
        try {
            out = new FileWriter(path);  //写入文件
            OutputFormat format = OutputFormat.createPrettyPrint();//缩减型格式
            //OutputFormat format = OutputFormat.createCompactFormat();//紧凑型格式
            format.setEncoding("UTF-8");//设置编码
            //format.setTrimText(false);//设置text中是否要删除其中多余的空格
            XMLWriter xw=new XMLWriter(out,format);
            xw.write(document);


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out!=null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }

    public static void creatXmlFile() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement( "edm" );
        root.addElement("install").addText("2");
        root.addElement("devinfo").addText("0");
        root.addElement("usbmode").addText("0");

        writer(document,PathConstants.INSTALL_STATE_PATH);

    }

你可能感兴趣的:(android)