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

  • 相关阅读:
    SpringBoot系列之切换log4j日志框架
    SpringBoot系列之日志框架使用教程
    SpringBoot系列之集成logback实现日志打印(篇二)
    源码学习系列之SpringBoot自动配置(篇二)
    SpringBoot系列之@Conditional注解用法简介
    7.Maven命令
    6.Maven构建过程的各个环节
    5.Maven坐标
    4.用IntelliJ IDEA 创建Maven Web
    3.用IntelliJ IDEA 创建Maven
  • 原文地址:https://www.cnblogs.com/lyxy/p/4569218.html
Copyright © 2011-2022 走看看