Class XmlDiff
java.lang.Object
cloud.opencode.base.xml.diff.XmlDiff
XML Diff - Compares two XML documents and produces a list of differences
XML 差异比较 - 比较两个 XML 文档并生成差异列表
This utility class provides methods for comparing XML documents structurally, detecting added, removed, and modified elements, attributes, and text content.
此工具类提供结构化比较 XML 文档的方法,检测新增、删除和修改的元素、属性和文本内容。
Features | 主要功能:
- Compare XML strings or XmlDocument objects - 比较 XML 字符串或 XmlDocument 对象
- Detect element, attribute, and text changes - 检测元素、属性和文本变更
- XPath-like path output for each difference - 为每个差异生成类 XPath 路径
- Equality check for quick comparison - 快速相等性检查
Usage Examples | 使用示例:
// Compare two XML strings
List<DiffEntry> diffs = XmlDiff.diff(xml1, xml2);
for (DiffEntry entry : diffs) {
System.out.println(entry.path() + " " + entry.type());
}
// Check equality
boolean equal = XmlDiff.isEqual(xml1, xml2);
Performance | 性能特性:
- Time complexity: O(n) where n is the total number of nodes - 时间复杂度: O(n),n 为节点总数
- Space complexity: O(n) for the diff result list - 空间复杂度: O(n),用于差异结果列表
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
- Null-safe: No (throws on null input) - 空值安全: 否(null 输入抛异常)
- Secure parsing via DomParser (XXE protection) - 通过 DomParser 安全解析(XXE 防护)
- Since:
- JDK 25, opencode-base-xml V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondiff(XmlDocument doc1, XmlDocument doc2) Compares two XmlDocument objects and returns a list of differences.Compares two XML strings and returns a list of differences.static booleanisEqual(XmlDocument doc1, XmlDocument doc2) Checks whether two XmlDocument objects are structurally equal.static booleanChecks whether two XML strings are structurally equal.
-
Method Details
-
diff
Compares two XML strings and returns a list of differences. 比较两个 XML 字符串并返回差异列表。- Parameters:
xml1- the first XML string | 第一个 XML 字符串xml2- the second XML string | 第二个 XML 字符串- Returns:
- the list of differences | 差异列表
- Throws:
OpenXmlException- if parsing fails | 如果解析失败则抛出异常
-
diff
Compares two XmlDocument objects and returns a list of differences. 比较两个 XmlDocument 对象并返回差异列表。- Parameters:
doc1- the first document | 第一个文档doc2- the second document | 第二个文档- Returns:
- the list of differences | 差异列表
-
isEqual
Checks whether two XML strings are structurally equal. 检查两个 XML 字符串是否结构相等。- Parameters:
xml1- the first XML string | 第一个 XML 字符串xml2- the second XML string | 第二个 XML 字符串- Returns:
- true if equal | 如果相等则返回 true
- Throws:
OpenXmlException- if parsing fails | 如果解析失败则抛出异常
-
isEqual
Checks whether two XmlDocument objects are structurally equal. 检查两个 XmlDocument 对象是否结构相等。- Parameters:
doc1- the first document | 第一个文档doc2- the second document | 第二个文档- Returns:
- true if equal | 如果相等则返回 true
-