Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

      • 简介
      • XML工具-XmlUtil
        • 1. XML解析
          • 1.1 从字符串解析XML
          • 1.2 从文件解析XML
          • 1.3 从URL解析XML
        • 2. XML生成
          • 2.1 生成XML字符串
          • 2.2 生成XML文件
        • 3. XML转换
          • 3.1 XML与Map转换
          • 3.2 XML与Java对象转换
        • 4. XML查询
          • 4.1 使用XPath查询
          • 4.2 使用节点路径查询
        • 5. XML操作
          • 5.1 添加节点
          • 5.2 修改节点
          • 5.3 删除节点
        • 6. XML格式化
          • 6.1 格式化XML字符串
          • 6.2 压缩XML字符串
        • 7. XML验证
          • 7.1 验证XML格式
          • 7.2 DTD验证
          • 7.3 XSD验证
        • 8. 适用场景
  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • XML操作(xml)
Hutool
2026-01-16
目录

XML工具-XmlUtil

# XML工具-XmlUtil

XmlUtil是Hutool中用于处理XML数据的核心工具类,提供了丰富的XML解析、生成、转换、查询等功能,让XML操作变得更加便捷和强大。

# 1. XML解析

# 1.1 从字符串解析XML

// 从字符串解析XML
String xmlStr = "<root><child>content</child></root>";
Document document = XmlUtil.readXML(xmlStr);

# 1.2 从文件解析XML

// 从文件解析XML
File xmlFile = new File("test.xml");
Document document = XmlUtil.readXML(xmlFile);

// 从输入流解析XML
InputStream inputStream = new FileInputStream(xmlFile);
Document document = XmlUtil.readXML(inputStream);

# 1.3 从URL解析XML

// 从URL解析XML
URL url = new URL("https://example.com/test.xml");
Document document = XmlUtil.readXML(url);

# 2. XML生成

# 2.1 生成XML字符串

// 创建Document对象
Document document = XmlUtil.createXml();
Element root = document.createElement("root");
document.appendChild(root);
Element child = document.createElement("child");
child.setTextContent("content");
root.appendChild(child);

// 生成XML字符串
String xmlStr = XmlUtil.toStr(document);

// 生成格式化的XML字符串
String formattedXml = XmlUtil.toStr(document, true);

# 2.2 生成XML文件

// 生成XML文件
XmlUtil.toFile(document, new File("output.xml"));

// 生成格式化的XML文件
XmlUtil.toFile(document, new File("formatted_output.xml"), true);

# 3. XML转换

# 3.1 XML与Map转换

// XML转换为Map
String xmlStr = "<root><name>张三</name><age>18</age></root>";
Map<String, Object> map = XmlUtil.xmlToMap(xmlStr);

// Map转换为XML
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("name", "张三");
dataMap.put("age", 18);
String xmlStr = XmlUtil.mapToXml(dataMap, "root");

# 3.2 XML与Java对象转换

// 定义Java对象
public class User {
    private String name;
    private int age;
    // getter/setter
}

// XML转换为Java对象
String xmlStr = "<User><name>张三</name><age>18</age></User>";
User user = XmlUtil.xmlToBean(xmlStr, User.class);

// Java对象转换为XML
User user = new User();
user.setName("张三");
user.setAge(18);
String xmlStr = XmlUtil.beanToXml(user);

# 4. XML查询

# 4.1 使用XPath查询

// 使用XPath查询单个节点
String xmlStr = "<root><users><user><name>张三</name></user><user><name>李四</name></user></users></root>";
Document document = XmlUtil.readXML(xmlStr);
Element userElement = XmlUtil.getNodeByXPath("//user[1]", document);

// 使用XPath查询多个节点
List<Element> userElements = XmlUtil.getNodesByXPath("//user", document);

// 使用XPath查询节点文本
String userName = XmlUtil.getNodeTextByXPath("//user[1]/name", document);

# 4.2 使用节点路径查询

// 获取根节点
Element root = XmlUtil.getRootElement(document);

// 获取子节点
List<Element> childElements = XmlUtil.getElements(root, "child");

// 获取单个子节点
Element childElement = XmlUtil.getElement(root, "child");

// 获取节点文本
String text = XmlUtil.getElementText(childElement);

# 5. XML操作

# 5.1 添加节点

// 添加子节点
Element newChild = document.createElement("newChild");
newChild.setTextContent("new content");
XmlUtil.appendChild(root, newChild);

// 添加属性
XmlUtil.addAttribute(childElement, "attrName", "attrValue");

# 5.2 修改节点

// 修改节点文本
XmlUtil.setElementText(childElement, "modified content");

// 修改节点属性
XmlUtil.setAttribute(childElement, "attrName", "modifiedValue");

# 5.3 删除节点

// 删除节点
XmlUtil.removeElement(childElement);

// 删除节点属性
XmlUtil.removeAttribute(childElement, "attrName");

# 6. XML格式化

# 6.1 格式化XML字符串

// 格式化XML字符串
String unformattedXml = "<root><child>content</child></root>";
String formattedXml = XmlUtil.formatXml(unformattedXml);

# 6.2 压缩XML字符串

// 压缩XML字符串(去除空格和换行)
String formattedXml = "<root>\n  <child>content</child>\n</root>";
String compressedXml = XmlUtil.compactXml(formattedXml);

# 7. XML验证

# 7.1 验证XML格式

// 验证XML格式是否正确
String xmlStr = "<root><child>content</child></root>";
boolean isValid = XmlUtil.isValidXml(xmlStr);

# 7.2 DTD验证

// 使用DTD验证XML
String xmlStr = "<!DOCTYPE root SYSTEM 'test.dtd'><root><child>content</child></root>";
boolean isValid = XmlUtil.validateByDtd(xmlStr);

# 7.3 XSD验证

// 使用XSD验证XML
String xmlStr = "<root><child>content</child></root>";
String xsdStr = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='root'><xs:complexType><xs:sequence><xs:element name='child' type='xs:string'/></xs:sequence></xs:complexType></xs:element></xs:schema>";
boolean isValid = XmlUtil.validateByXsd(xmlStr, xsdStr);

# 8. 适用场景

XmlUtil适用于以下场景:

  • 需要解析XML文档时
  • 需要生成XML文档时
  • 需要进行XML与Java对象之间的转换时
  • 需要进行XML与Map之间的转换时
  • 需要在XML文档中进行查询和选择时
  • 需要修改XML文档内容时
  • 需要格式化或压缩XML字符串时
  • 需要验证XML格式是否正确时

通过XmlUtil,开发者可以更加便捷地处理XML数据,编写更简洁、更易维护的代码,同时享受到更丰富的XML操作功能和更高的安全性。

简介
简介

← 简介 简介→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式