Live2D

java 修改带有属性的xml 【xpath】

需求:修改带有属性的xml  如需要修改下面title【一般不带有属性的修改方式比较容易,在此不演示】

1
2
3
4
5
6
<book category="COOKING">
  <title lang="en">Everyday Italian</title> 
  <author>Giada De Laurentiis</author> 
  <year>2005</year> 
  <price>30.00</price>
</book>

准备:

一、jar包,这两个jar包可以用maven的porm引用也可以直接去网上下载(链接:https://pan.baidu.com/s/1V2f-s3FnEkkvmy--eD3-cw提取码:hk26)。

dom4j-1.6.1.jar
jaxen-1.1-beta-8.jar
1
2
3
4
5
6
7
8
9
10
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>1.1-beta-8</version>
</dependency>

 

二、xml文件,这里使用xpah定位元素

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
 
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title> 
    <author>Giada De Laurentiis</author> 
    <year>2005</year> 
    <price>30.00</price>
  </book> 
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title> 
    <author>J K. Rowling</author> 
    <year>2005</year> 
    <price>29.99</price>
  </book> 
  <book category="WEB">
    <title lang="en">啦啦啦</title> 
    <author>James McGovern</author> 
    <author>Per Bothner</author> 
    <author>Kurt Cagle</author> 
    <author>James Linn</author> 
    <author>Vaidyanathan Nagarajan</author> 
    <year>2003</year> 
    <price>49.99</price>
  </book> 
  <book category="WEB">
    <title lang="en">Learning XML</title> 
    <author>Erik T. Ray</author> 
    <year>2003</year> 
    <price>39.95</price>
  </book>
</bookstore>

 

三、代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
 
public class demos1 {
    public static void main(String[] args) {
        String filePath = "E:\\Temp\\dd.xml";
        String xpath = "//book[@category='WEB']/title[@lang='en']";//查询属性type='Creator'
        String values = "啦啦啦";
        updateXml(filePath, xpath, values);
    }
 
    public static String updateXml(String filePath, String xpath, String values) {
        try {
            //获取tomcat下文件的相对路径
            SAXReader reader = new SAXReader();
//            FileInputStream f = new FileInputStream(filePath);
            File file=new File(filePath);
            Document document = reader.read(file); //dom4j读取
            Element element = (Element) document.selectSingleNode(xpath);//得到name=Creator的元素
            element.setText(values);
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            writer.write(document);
            writer.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "OK!";
    }
}

  

 

posted @   -涂涂-  阅读(264)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示