Class XmlDocument
java.lang.Object
cloud.opencode.base.xml.XmlDocument
XML Document - Wrapper for DOM Document with fluent API
XML 文档 - 提供流式 API 的 DOM Document 封装
This class wraps a DOM Document and provides convenient methods for parsing, querying, modifying, and serializing XML content.
此类封装 DOM Document,并提供便捷的方法来解析、查询、修改和序列化 XML 内容。
Features | 主要功能:
- Parse XML from String, File, InputStream, classpath resource - 从字符串、文件、输入流、类路径资源解析 XML
- XPath query support - XPath 查询支持
- Element access and modification - 元素访问和修改
- Object binding via annotations - 通过注解进行对象绑定
- Serialization to XML string or file - 序列化为 XML 字符串或文件
- Conversion to Map representation - 转换为 Map 表示
Usage Examples | 使用示例:
// Parse XML string
XmlDocument doc = XmlDocument.parse("<root><item>value</item></root>");
// Load from file
XmlDocument doc = XmlDocument.load(Path.of("config.xml"));
// Create new document
XmlDocument doc = XmlDocument.create("root");
// Access content
XmlElement root = doc.getRoot();
String text = doc.getElementText("item");
// XPath queries
String value = doc.xpath("//item/text()");
List<XmlElement> items = doc.xpathList("//item");
// Bind to object
Config config = doc.bind(Config.class);
// Serialize
String xml = doc.toXml(4); // with 4-space indent
doc.save(Path.of("output.xml"));
Security | 安全性:
- Thread-safe: No (wraps mutable DOM Document) - 线程安全: 否(封装可变的 DOM Document)
- Null-safe: No (null inputs throw exceptions) - 空值安全: 否(空值输入抛出异常)
- Secure parsing with XXE protection - 安全解析,带 XXE 防护
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddElement(XmlElement element) Adds an existing element to the root.addElement(String name, String text) Adds an element to the root.<T> TBinds this document to an object.static XmlDocumentCreates an empty document with the specified root element.Returns the underlying DOM Document.getElement(String name) Returns the first element with the specified name.getElements(String name) Returns all elements with the specified name.getElementText(String name) Returns the text content of the first element with the specified name.getElementText(String name, String defaultValue) Returns the text content of an element or default value.getRoot()Returns the root element.booleanhasElement(String name) Returns whether an element with the specified name exists.static XmlDocumentload(InputStream input) Loads XML from input stream.static XmlDocumentLoads XML from file.static XmlDocumentloadResource(String resourceName) Loads XML from classpath resource.static XmlDocumentWraps an existing DOM Document.static XmlDocumentParses XML string to document.removeElement(String name) Removes elements with the specified name from the root.voidSaves this document to file.toMap()Converts this document to a Map.toString()toXml()Converts this document to XML string.toXml(int indent) Converts this document to formatted XML string.Evaluates an XPath expression and returns string result.<T> TEvaluates an XPath expression and returns typed result.Evaluates an XPath expression and returns element list.Evaluates an XPath expression and returns single element.
-
Method Details
-
parse
Parses XML string to document. 解析 XML 字符串为文档。- Parameters:
xml- the XML string | XML 字符串- Returns:
- the document | 文档
-
load
Loads XML from file. 从文件加载 XML。- Parameters:
path- the file path | 文件路径- Returns:
- the document | 文档
-
load
Loads XML from input stream. 从输入流加载 XML。- Parameters:
input- the input stream | 输入流- Returns:
- the document | 文档
-
loadResource
Loads XML from classpath resource. 从类路径资源加载 XML。- Parameters:
resourceName- the resource name | 资源名- Returns:
- the document | 文档
-
create
Creates an empty document with the specified root element. 创建具有指定根元素的空文档。- Parameters:
rootName- the root element name | 根元素名称- Returns:
- the document | 文档
-
of
Wraps an existing DOM Document. 封装现有的 DOM Document。- Parameters:
document- the DOM Document | DOM Document- Returns:
- the wrapped document | 封装的文档
-
getRoot
-
getDocument
Returns the underlying DOM Document. 返回底层的 DOM Document。- Returns:
- the DOM Document | DOM Document
-
xpath
-
xpathList
Evaluates an XPath expression and returns element list. 求值 XPath 表达式并返回元素列表。- Parameters:
xpath- the XPath expression | XPath 表达式- Returns:
- the list of matching elements | 匹配元素列表
-
xpathOne
Evaluates an XPath expression and returns single element. 求值 XPath 表达式并返回单个元素。- Parameters:
xpath- the XPath expression | XPath 表达式- Returns:
- the element, or null if not found | 元素,如果未找到则返回 null
-
xpath
-
getElement
Returns the first element with the specified name. 返回具有指定名称的第一个元素。- Parameters:
name- the element name | 元素名称- Returns:
- the element, or null if not found | 元素,如果未找到则返回 null
-
getElements
Returns all elements with the specified name. 返回具有指定名称的所有元素。- Parameters:
name- the element name | 元素名称- Returns:
- the list of elements | 元素列表
-
getElementText
-
getElementText
-
hasElement
Returns whether an element with the specified name exists. 返回是否存在具有指定名称的元素。- Parameters:
name- the element name | 元素名称- Returns:
- true if element exists | 如果元素存在则返回 true
-
addElement
Adds an element to the root. 向根元素添加元素。- Parameters:
name- the element name | 元素名称text- the text content | 文本内容- Returns:
- the new element | 新元素
-
addElement
Adds an existing element to the root. 向根元素添加现有元素。- Parameters:
element- the element to add | 要添加的元素- Returns:
- this document for chaining | 此文档以便链式调用
-
removeElement
Removes elements with the specified name from the root. 从根元素移除具有指定名称的元素。- Parameters:
name- the element name | 元素名称- Returns:
- this document for chaining | 此文档以便链式调用
-
bind
Binds this document to an object. 将此文档绑定到对象。- Type Parameters:
T- the type parameter | 类型参数- Parameters:
clazz- the target type | 目标类型- Returns:
- the bound object | 绑定的对象
-
toXml
Converts this document to XML string. 将此文档转换为 XML 字符串。- Returns:
- the XML string | XML 字符串
-
toXml
Converts this document to formatted XML string. 将此文档转换为格式化的 XML 字符串。- Parameters:
indent- the number of spaces for indentation | 缩进空格数- Returns:
- the formatted XML string | 格式化的 XML 字符串
-
save
Saves this document to file. 将此文档保存到文件。- Parameters:
path- the file path | 文件路径
-
toMap
-
toString
-