Class DomUtil
java.lang.Object
cloud.opencode.base.xml.dom.DomUtil
DOM Utility Class - Utility methods for DOM manipulation
DOM 工具类 - DOM 操作的工具方法
This class provides static utility methods for common DOM operations including serialization, conversion, and node manipulation.
此类提供用于常见 DOM 操作的静态工具方法,包括序列化、转换和节点操作。
Features | 主要功能:
- Document/Node to String conversion - 文档/节点转字符串
- Element to Map conversion - 元素转 Map
- Text type conversion - 文本类型转换
- Node cloning and manipulation - 节点克隆和操作
Usage Examples | 使用示例:
// Convert document to string
String xml = DomUtil.toString(document, 4);
// Convert element to map
Map<String, String> map = DomUtil.toMap(element);
// Get text content with type conversion
int value = DomUtil.getInt(element, "count", 0);
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
- Null-safe: No (throws on null document/node) - 空值安全: 否(null 文档/节点抛异常)
Performance | 性能特性:
- Time complexity: O(n) for elementToMap/toMap (recursive traversal of all nodes, depth-limited to 1000); O(c) for getChildElements where c=child count; O(1) for scalar getters - 时间复杂度: elementToMap/toMap 为 O(n)(递归遍历所有节点,深度限制为 1000);getChildElements 为 O(c),c 为子元素数;标量获取器为 O(1)
- Space complexity: O(n) for map conversions; O(1) for scalar operations - 空间复杂度: map 转换为 O(n);标量操作为 O(1)
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic ElementaddChildElement(Element parent, String tagName, String text) Adds a child element with text content.static NodeDeep clones a node into another document.static <T> TconvertText(String text, Class<T> clazz) Converts text to the specified type.static DocumentCreates a new empty Document.static DocumentcreateDocument(String rootElementName) Creates a new Document with a root element.elementToMap(Element element) Converts an Element to a Map.elementToSimpleMap(Element element) Converts a flat, single-level XML element to aMap<String, String>.getChildElements(Node parent) Gets all child elements of a node.getChildElements(Node parent, String tagName) Gets child elements with the specified tag name.static StringgetChildText(Node parent, String tagName) Gets the text content of the first child element with the specified tag name.static ElementgetFirstChildElement(Node parent, String tagName) Gets the first child element with the specified tag name.static voidremoveAllChildren(Node node) Removes all child nodes from a node.static StringConverts a Document to XML string.static StringConverts a Document to formatted XML string.static StringConverts a Node to XML string.static StringConverts a Node to formatted XML string.
-
Method Details
-
createDocument
Creates a new empty Document. 创建新的空 Document。- Returns:
- a new empty Document | 新的空 Document
-
createDocument
-
toString
-
toString
-
toString
-
toString
-
elementToMap
-
convertText
-
elementToSimpleMap
Converts a flat, single-level XML element to aMap<String, String>. 将单层 XML 元素转换为Map<String, String>。Only direct child elements are included; attributes and nested child elements are ignored. CDATA sections are automatically merged into text content by the DOM parser, so no manual stripping is needed.
仅包含直接子元素;属性和嵌套子元素被忽略。DOM 解析器自动合并 CDATA 节, 无需手动剥离。
The returned map is a
TreeMap(naturally ordered by key), which is required by protocols that build deterministic signing strings (e.g. WeChat Pay V2).返回
TreeMap(按 key 自然排序),满足需要确定性签名串的协议 (如微信支付 V2)。- Parameters:
element- the root element whose direct children become map entries | 根元素,其直接子元素成为 map 条目- Returns:
- a sorted map of tag-name → text-content | 按标签名排序的 map
- Since:
- V1.0.4
-
cloneInto
-
getChildElements
-
getChildElements
-
getFirstChildElement
-
getChildText
-
removeAllChildren
Removes all child nodes from a node. 从节点移除所有子节点。- Parameters:
node- the node to clear | 要清空的节点
-
addChildElement
-