Class DomBuilder
java.lang.Object
cloud.opencode.base.xml.dom.DomBuilder
DOM Builder - Builder for creating DOM documents programmatically
DOM 构建器 - 程序化创建 DOM 文档的构建器
This class provides a fluent API for building DOM documents without parsing XML strings.
此类提供流式 API,无需解析 XML 字符串即可构建 DOM 文档。
Features | 主要功能:
- Programmatic DOM document construction without parsing - 无需解析即可程序化构建 DOM 文档
- Fluent chaining API for elements, attributes, text, CDATA, comments - 元素、属性、文本、CDATA、注释的流式链式 API
- Namespace-aware element creation - 支持命名空间的元素创建
- Node import from other documents - 从其他文档导入节点
- Processing instruction support - 处理指令支持
Usage Examples | 使用示例:
Document doc = DomBuilder.create("root")
.addElement("name", "John")
.addElement("age", "30")
.startElement("address")
.addElement("city", "Beijing")
.addElement("country", "China")
.endElement()
.build();
Security | 安全性:
- Thread-safe: No (mutable builder state) - 线程安全: 否(可变构建器状态)
- Null-safe: Partial (null text values are allowed and skipped) - 空值安全: 部分(空文本值被允许并跳过)
Performance | 性能特性:
- Time complexity: O(n) for complete document construction where n=total elements; each addElement/startElement call is O(1) - 时间复杂度: 完整文档构建为 O(n),n 为元素总数;每次 addElement/startElement 调用为 O(1)
- Space complexity: O(n) for the DOM tree in memory - 空间复杂度: 内存中 DOM 树为 O(n)
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddElement(String name, String text) Adds a child element with text content to the current element.addElement(String name, String text, String attrName, String attrValue) Adds a child element with text content and attribute.Adds an attribute to the current element.build()Builds and returns the Document.Adds a CDATA section to the current element.Adds a comment to the current element.static DomBuilderCreates a new DOM builder with the specified root element name.static DomBuilderCreates a new DOM builder with namespace.Ends the current element and returns to its parent.Gets the underlying Document (alias for build()).Gets the root element.importNode(Node node, boolean deep) Imports and appends a node from another document.processingInstruction(String target, String data) Adds a processing instruction to the document.Returns to the root element.startElement(String name) Starts a new child element and makes it the current element.startElement(String name, String namespaceUri) Starts a new child element with namespace.Sets the text content of the current element.toXml()Builds and returns the Document as XML string.toXml(int indent) Builds and returns the Document as formatted XML string.
-
Method Details
-
create
Creates a new DOM builder with the specified root element name. 使用指定的根元素名称创建新的 DOM 构建器。- Parameters:
rootName- the root element name | 根元素名称- Returns:
- a new builder | 新构建器
-
create
Creates a new DOM builder with namespace. 创建带命名空间的新 DOM 构建器。- Parameters:
rootName- the root element name | 根元素名称namespaceUri- the namespace URI | 命名空间 URI- Returns:
- a new builder | 新构建器
-
attribute
Adds an attribute to the current element. 向当前元素添加属性。- Parameters:
name- the attribute name | 属性名value- the attribute value | 属性值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
addElement
Adds a child element with text content to the current element. 向当前元素添加带文本内容的子元素。- Parameters:
name- the element name | 元素名称text- the text content | 文本内容- Returns:
- this builder for chaining | 此构建器以便链式调用
-
addElement
Adds a child element with text content and attribute. 添加带文本内容和属性的子元素。- Parameters:
name- the element name | 元素名称text- the text content | 文本内容attrName- the attribute name | 属性名attrValue- the attribute value | 属性值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
startElement
Starts a new child element and makes it the current element. 开始新的子元素并将其设为当前元素。- Parameters:
name- the element name | 元素名称- Returns:
- this builder for chaining | 此构建器以便链式调用
-
startElement
Starts a new child element with namespace. 开始带命名空间的新子元素。- Parameters:
name- the element name | 元素名称namespaceUri- the namespace URI | 命名空间 URI- Returns:
- this builder for chaining | 此构建器以便链式调用
-
endElement
Ends the current element and returns to its parent. 结束当前元素并返回其父元素。- Returns:
- this builder for chaining | 此构建器以便链式调用
-
text
Sets the text content of the current element. 设置当前元素的文本内容。- Parameters:
text- the text content | 文本内容- Returns:
- this builder for chaining | 此构建器以便链式调用
-
cdata
Adds a CDATA section to the current element. 向当前元素添加 CDATA 节。- Parameters:
text- the CDATA content | CDATA 内容- Returns:
- this builder for chaining | 此构建器以便链式调用
-
comment
Adds a comment to the current element. 向当前元素添加注释。- Parameters:
comment- the comment text | 注释文本- Returns:
- this builder for chaining | 此构建器以便链式调用
-
processingInstruction
Adds a processing instruction to the document. 向文档添加处理指令。- Parameters:
target- the target | 目标data- the data | 数据- Returns:
- this builder for chaining | 此构建器以便链式调用
-
importNode
Imports and appends a node from another document. 从另一个文档导入并附加节点。- Parameters:
node- the node to import | 要导入的节点deep- whether to import children | 是否导入子节点- Returns:
- this builder for chaining | 此构建器以便链式调用
-
returnToRoot
Returns to the root element. 返回根元素。- Returns:
- this builder for chaining | 此构建器以便链式调用
-
getDocument
Gets the underlying Document (alias for build()). 获取底层 Document(build() 的别名)。- Returns:
- the Document | Document
-
getRootElement
-
build
Builds and returns the Document. 构建并返回 Document。- Returns:
- the built Document | 构建的 Document
-
toXml
Builds and returns the Document as XML string. 构建并返回 Document 为 XML 字符串。- Returns:
- the XML string | XML 字符串
-
toXml
Builds and returns the Document as formatted XML string. 构建并返回 Document 为格式化的 XML 字符串。- Parameters:
indent- the number of spaces for indentation | 缩进空格数- Returns:
- the formatted XML string | 格式化的 XML 字符串
-