Dom4j操作xml文件

Dom4j操作xml文件实现简单增删改查

引入依赖

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

准备一个xml文件 book.xml

<books>
    <book author="天蚕土豆">
        <name>斗破苍穹</name>
        <price>86</price>
    </book>
    <book author="萧潜">
        <name>缥缈之旅</name>
        <price>92</price>
    </book>
    <book author="萧鼎">
        <name>诛仙</name>
        <price>98</price>
    </book>
    <book author="天下霸唱">
        <name>鬼吹灯</name>
        <price>124</price>
    </book>
    <book author="辰东">
        <name>神墓</name>
        <price>128</price>
    </book>
</books>

测试

@Test
void contextLoads() {

    String fileName = "D:\\IDEA\\xmlTest\\src\\main\\resources\\books.xml";

    try {
        //1.将xml文件加载到内存中
        SAXReader saxReader = new SAXReader();
        //document 加载执行xml文档获取Document对象
        Document document = saxReader.read(fileName);
        //2.获取根元素的对象 -- books getRootElement 获取根元素
        Element rootElement = document.getRootElement();

        /*
            添加
             */
        Element book1 = rootElement.addElement("book");
        book1.addAttribute("author", "江南");
        Element name1 = book1.addElement("name");
        name1.addText("龙族");
        Element price1 = book1.addElement("price");
        price1.addText("199");

        /*
            修改
             */
        List<Element> elements2 = rootElement.elements();
        Element book2 = elements2.get(2);
        book2.addAttribute("author","爱潜水的乌贼");
        Element name2 = book2.element("name");
        name2.setText("诡秘之主");
        Element price2 = book2.element("price");
        price2.setText("300");

        /*
            删除
             */
        Element book3=(Element) rootElement.elements().get(0);
        //得到属性对象
        Attribute idAttr=book3.attribute("author");
        //删除属性
        idAttr.detach();
        //删除整个元素
        book3.detach();


        //每个元素的对象 都是Element对象
        List<Element> elements = rootElement.elements();
        //3.使用根元素的对象  对其他元素操作操作
        //3.1获取所有的子元素对象 book
        for (Element book : elements) {
            //在循环内拿到了 每个 book  元素对象
            //3.2获取book的属性值
            String author = book.attributeValue("author");
            //3.3获取book的子元素  name price
            String name = book.element("name").getText();
            String price = book.element("price").getText();
            System.out.println(name + " " + author + " " + price);
        }
        //指定文件输出的位置
        FileOutputStream out = new FileOutputStream(fileName);

        // 指定文本的写出的格式:
        OutputFormat format = OutputFormat.createPrettyPrint();   //漂亮格式:有空格换行
        format.setEncoding("UTF-8");

        //1.创建写出对象
        XMLWriter writer = new XMLWriter(out, format);

        //2.写出Document对象
        writer.write(document);

        //3.关闭流
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

结果

控制台
缥缈之旅 萧潜 92
诡秘之主 爱潜水的乌贼 300
鬼吹灯 天下霸唱 124
神墓 辰东 128
龙族 江南 199
xml文件
<?xml version="1.0" encoding="UTF-8"?>

<books> 
  <book author="萧潜"> 
    <name>缥缈之旅</name>  
    <price>92</price> 
  </book>  
  <book author="爱潜水的乌贼"> 
    <name>诡秘之主</name>  
    <price>300</price> 
  </book>  
  <book author="天下霸唱"> 
    <name>鬼吹灯</name>  
    <price>124</price> 
  </book>  
  <book author="辰东"> 
    <name>神墓</name>  
    <price>128</price> 
  </book>  
  <book author="江南">
    <name>龙族</name>
    <price>199</price>
  </book>
</books>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值