zoukankan      html  css  js  c++  java
  • dom4j读写XML文件

    XML文件格式:

    <?xml version="1.0" encoding="UTF-8"?>
    <company> 
        <employee name="zhangsan" value="张三"/>  
        <employee name="lisi" value="李四"/>  
        <employee name="wangwu" value="王五"/>  
    </company>

    dom4j读写方法:

    /**
         * 读取
         */
        public static void read() {
            try {
                File xmlFile = new File("d://company.xml");
                Document document = new SAXReader().read(xmlFile);
                List<Element> elements = document.selectNodes("/company/employee");
                for (Element element : elements) {
                    String name = element.attributeValue("name");
                    String value = element.attributeValue("value");
                    System.out.println(name + " = " + value);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 写入
         */
        public static void write() {
            FileOutputStream fileOutputStream = null;
            XMLWriter xmlWriter = null;
            try {
                File xmlFile = new File("d://company.xml");
                Document document = new SAXReader().read(xmlFile);
                // 获取根节点
                Element root = document.getRootElement();
                // 动态添加 <employee name="xiaohong" value="小红"/>
                Element employee = root.addElement("employee");
                employee.addAttribute("name", "xiaohong");
                employee.addAttribute("value", "小红");
                OutputFormat outputFormat = OutputFormat.createPrettyPrint();
                outputFormat.setEncoding("UTF-8");
                outputFormat.setIndent(true);
                outputFormat.setIndent("    ");// tab格
                outputFormat.setNewlines(true);// 换行
                fileOutputStream = new FileOutputStream(xmlFile);
                xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
                xmlWriter.write(document);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (xmlWriter != null) {
                    try {
                        xmlWriter.close();
                    } catch (IOException e) {
                    }
                }
            }
    
        }

    写入后XML格式:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <company> 
        <employee name="zhangsan" value="张三"/>  
        <employee name="lisi" value="李四"/>  
        <employee name="wangwu" value="王五"/>  
        <employee name="xiaohong" value="小红"/>
    </company>

     注:所需包 dom4j-1.6.1.jar、jaxen-1.1.1.jar

  • 相关阅读:
    GDI+ 实现透明水印和文字
    delphi调用LUA函数来处理一些逻辑
    Delphi 不使用自带模板创建服务
    Delphi在Listview中加入Edit控件
    中文转码器的工作原理_delphi教程
    使用钩子函数[6]
    简单全局HOOK拦截大部分键盘消息
    4个字节就相当于移动一位,原来指针是这样用的
    C#调用Delphi接口(ITest = interface)
    DELPHI 对象的本质 VMT
  • 原文地址:https://www.cnblogs.com/lyxy/p/4569218.html
Copyright © 2011-2022 走看看