 XML 工具类 - XmlUtil
XML 工具类 - XmlUtil
 # XML 工具类 - XmlUtil
 简介
XmlUtil 是 Hutool 提供的一个用于简化 XML 处理的工具类。虽然 JDK 已经提供了 W3C DOM 的 XML 解析和构建工具,但其 API 操作比较繁琐,因此 XmlUtil 对 XML 的创建、读取、写入、操作进行了简化封装,使开发者能够更方便地处理 XML 数据,尤其是在配置文件管理、接口通信等场景中。
# 1. 读取 XML
XmlUtil 提供了读取 XML 文件和解析 XML 字符串为 Document 对象的功能,适合处理各种来源的 XML 数据。
示例代码:
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import java.io.File;
public class XmlUtilExample {
    public static void main(String[] args) {
        // 定义 XML 文件路径
        File xmlFile = new File("example.xml");
        // 读取 XML 文件并解析为 Document 对象
        Document document = XmlUtil.readXML(xmlFile);
        System.out.println("XML Document: " + document);
    }
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
参数说明:
- readXML(File file):读取 XML 文件并解析为- Document对象。- file:要读取的 XML 文件,不能为- null。
- 返回值:解析得到的 Document对象。
 
开发场景:
- 在需要处理配置文件、数据文件等场景下,可以使用此方法读取 XML 文件内容,并将其解析为可操作的文档对象。
# 2. 解析 XML 字符串
对于从网络、接口等获取的 XML 数据,XmlUtil 提供了将 XML 字符串解析为 Document 对象的功能。
示例代码:
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
public class XmlUtilExample {
    public static void main(String[] args) {
        // 定义 XML 字符串
        String xmlStr = "<root><element>value</element></root>";
        // 解析 XML 字符串为 Document 对象
        Document document = XmlUtil.parseXml(xmlStr);
        System.out.println("XML Document: " + document);
    }
}
2
3
4
5
6
7
8
9
10
11
12
13
参数说明:
- parseXml(String xmlStr):将 XML 字符串解析为- Document对象。- xmlStr:要解析的 XML 字符串,不能为- null。
- 返回值:解析得到的 Document对象。
 
开发场景:
- 在处理从网络、接口等获取的 XML 数据时,可以使用此方法将字符串转换为 XML 文档对象,便于后续操作。
# 3. 写入 XML
XmlUtil 提供了将 Document 对象写入文件或转换为字符串的方法,方便进行 XML 数据的持久化存储或传输。
示例代码:
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import java.io.File;
public class XmlUtilExample {
    public static void main(String[] args) {
        // 创建一个空的 XML 文档
        Document document = XmlUtil.createXml();
        // 定义输出文件路径
        File outputFile = new File("output.xml");
        // 将 XML 文档写入文件
        XmlUtil.toFile(document, outputFile);
        System.out.println("XML 已写入文件: " + outputFile.getAbsolutePath());
    }
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
参数说明:
- toFile(Document document, File file):将- Document对象写入到文件中。- document:要写入的 XML 文档对象,不能为- null。
- file:目标文件,不能为- null。
- 返回值:无。
 
开发场景:
- 在需要将 XML 数据保存到文件系统中时,可以使用此方法进行持久化存储,如配置文件的导出和备份。
# 4. 创建 XML 文档
XmlUtil 提供了创建一个新的 XML 文档的功能,适合在需要动态生成 XML 数据的场景中使用。
示例代码:
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XmlUtilExample {
    public static void main(String[] args) {
        // 创建一个空的 XML 文档
        Document document = XmlUtil.createXml();
        // 创建根元素
        Element root = document.createElement("root");
        document.appendChild(root);
        // 在根元素中添加子元素
        Element child = document.createElement("child");
        child.setTextContent("value");
        root.appendChild(child);
        // 将 XML 文档转换为字符串
        String xmlStr = XmlUtil.toStr(document);
        System.out.println("创建的 XML 文档: " + xmlStr);
    }
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
参数说明:
- createXml():创建一个新的 XML 文档对象。- 返回值:空的 Document对象。
 
- 返回值:空的 
开发场景:
- 在需要动态生成 XML 数据时,如生成配置文件、接口请求等,可以使用此方法创建新的 XML 文档。
# 5. 操作 XML 节点
XmlUtil 提供了多种工具方法,用于对 XML 文档中的节点进行操作,如获取节点、获取节点文本值等。
示例代码:
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XmlUtilExample {
    public static void main(String[] args) {
        // 定义 XML 字符串
        String xmlStr = "<root><child>value</child></root>";
        // 解析 XML 字符串为 Document 对象
        Document document = XmlUtil.parseXml(xmlStr);
        // 获取第一个子节点
        Element childElement = XmlUtil.getElement(document.getDocumentElement(), "child");
        System.out.println("子节点: " + childElement);
        // 获取子节点的文本值
        String childText = XmlUtil.elementText(document.getDocumentElement(), "child");
        System.out.println("子节点的文本值: " + childText);
    }
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
参数说明:
- getElement(Element element, String tagName):根据节点名获取第一个子节点。- element:父元素,不能为- null。
- tagName:子节点的标签名,不能为- null。
- 返回值:获取到的子节点,可能为 null。
 
- elementText(Element element, String tagName):根据节点名获取第一个子节点的文本值。- element:父元素,不能为- null。
- tagName:子节点的标签名,不能为- null。
- 返回值:子节点的文本值,可能为 null。
 
开发场景:
- 在解析 XML 配置文件或数据文件时,使用这些方法可以方便地获取所需的节点和节点内容。
# 6. 对象与 XML 之间的转换
XmlUtil 提供了将对象与 XML 之间进行转换的方法,便于对象的序列化与反序列化操作。
# 写入对象到 XML 文件
import cn.hutool.core.util.XmlUtil;
import java.io.File;
import java.io.Serializable;
public class XmlUtilExample {
    public static void main(String[] args) {
        // 定义一个可序列化的对象
        Person person = new Person("John", 30);
        // 将对象写入 XML 文件
        XmlUtil.writeObjectAsXml(new File("person.xml"), person);
        System.out.println("对象已写入 XML 文件");
    }
    // 定义一个可序列化的类
    static class Person implements Serializable {
        private String name;
        private int age;
        // 构造方法
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
        // Getter 和 Setter 方法
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        // 重写 toString 方法
        @Override
        public String toString() {
            return "Person{name='" + name + "', age=" + age + "}";
        }
    }
}
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
41
42
43
44
45
46
47
48
49
50
参数说明:
- writeObjectAsXml(File file, Object obj):将可序列化的对象转换为 XML 并写入文件。- file:目标文件,不能为- null。
- obj:要转换为 XML 的对象,不能为- null。
- 返回值:无。
 
开发场景:
- 在配置管理、数据备
份和恢复中,可以使用此方法将对象保存为 XML 文件,便于数据持久化。
# 从 XML 文件读取对象
import cn.hutool.core.util.XmlUtil;
import java.io.File;
import java.io.Serializable;
public class XmlUtilExample {
    public static void main(String[] args) {
        // 从 XML 文件中读取对象
        Person personFromXml = XmlUtil.readObjectFromXml(new File("person.xml"));
        System.out.println("从 XML 文件中读取的对象: " + personFromXml);
    }
    // 定义一个可序列化的类
    static class Person implements Serializable {
        private String name;
        private int age;
        // 构造方法
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
        // Getter 和 Setter 方法
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        // 重写 toString 方法
        @Override
        public String toString() {
            return "Person{name='" + name + "', age=" + age + "}";
        }
    }
}
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
41
42
43
44
45
46
47
参数说明:
- readObjectFromXml(File file):从 XML 文件中读取对象。- file:包含 XML 数据的文件,不能为- null。
- 返回值:从 XML 中读取的对象。
 
开发场景:
- 在系统配置恢复、数据导入等场景中,可以使用此方法从 XML 文件中读取对象数据,便于数据的反序列化操作。
总结
XmlUtil 工具类在处理 XML 的创建、读取、写入和操作方面提供了强大且便捷的工具方法。它适用于多种场景,如配置管理、数据存储、接口通信等,极大地简化了 XML 数据的处理工作,提高了开发效率。
