Class XmlMerge
java.lang.Object
cloud.opencode.base.xml.merge.XmlMerge
XML Merge - Merges two XML documents by overlaying one onto another
XML 合并 - 通过将一个 XML 文档覆盖到另一个上来合并两个 XML 文档
This utility class provides methods for merging XML documents. The overlay document's elements are merged onto the base document. Matching elements (by tag name) have their text content and attributes replaced; non-matching elements from the overlay are appended.
此工具类提供合并 XML 文档的方法。覆盖文档的元素合并到基础文档上。 匹配的元素(按标签名)的文本内容和属性将被替换;覆盖文档中不匹配的元素将被追加。
Features | 主要功能:
- Merge XmlDocument objects or XML strings - 合并 XmlDocument 对象或 XML 字符串
- Recursive element merging by tag name - 按标签名递归合并元素
- Attribute overlay - 属性覆盖
- Configurable merge strategy - 可配置的合并策略
Usage Examples | 使用示例:
// Merge two XML documents
XmlDocument merged = XmlMerge.merge(base, overlay);
// Merge XML strings
String merged = XmlMerge.merge(baseXml, overlayXml);
// Merge with strategy
XmlDocument merged = XmlMerge.merge(base, overlay, MergeStrategy.APPEND);
Performance | 性能特性:
- Time complexity: O(n * m) where n and m are node counts - 时间复杂度: O(n * m),n 和 m 为节点数
- Space complexity: O(n + m) for the merged document - 空间复杂度: O(n + m),用于合并后的文档
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
- Null-safe: No (null inputs throw NullPointerException) - 空值安全: 否(null 输入抛出 NullPointerException)
- Secure parsing via SecureParserFactory - 通过 SecureParserFactory 安全解析
- Since:
- JDK 25, opencode-base-xml V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic XmlDocumentmerge(XmlDocument base, XmlDocument overlay) Merges an overlay document onto a base document using OVERRIDE strategy.static XmlDocumentmerge(XmlDocument base, XmlDocument overlay, MergeStrategy strategy) Merges an overlay document onto a base document with the specified strategy.static StringMerges two XML strings using OVERRIDE strategy and returns the merged XML string.static Stringmerge(String baseXml, String overlayXml, MergeStrategy strategy) Merges two XML strings using the specified strategy and returns the merged XML string.
-
Method Details
-
merge
Merges an overlay document onto a base document using OVERRIDE strategy. 使用 OVERRIDE 策略将覆盖文档合并到基础文档上。- Parameters:
base- the base document | 基础文档overlay- the overlay document | 覆盖文档- Returns:
- a new merged document | 新的合并文档
-
merge
Merges an overlay document onto a base document with the specified strategy. 使用指定策略将覆盖文档合并到基础文档上。- Parameters:
base- the base document | 基础文档overlay- the overlay document | 覆盖文档strategy- the merge strategy | 合并策略- Returns:
- a new merged document | 新的合并文档
-
merge
Merges two XML strings using OVERRIDE strategy and returns the merged XML string. 使用 OVERRIDE 策略合并两个 XML 字符串并返回合并后的 XML 字符串。- Parameters:
baseXml- the base XML string | 基础 XML 字符串overlayXml- the overlay XML string | 覆盖 XML 字符串- Returns:
- the merged XML string | 合并后的 XML 字符串
-
merge
Merges two XML strings using the specified strategy and returns the merged XML string. 使用指定策略合并两个 XML 字符串并返回合并后的 XML 字符串。- Parameters:
baseXml- the base XML string | 基础 XML 字符串overlayXml- the overlay XML string | 覆盖 XML 字符串strategy- the merge strategy | 合并策略- Returns:
- the merged XML string | 合并后的 XML 字符串
-