Class ElementBuilder
java.lang.Object
cloud.opencode.base.xml.builder.ElementBuilder
Element Builder - Fluent builder for XML elements
元素构建器 - XML 元素的流式构建器
This class provides a fluent API for building standalone XML elements.
此类提供流式 API,用于构建独立的 XML 元素。
Features | 主要功能:
- Fluent builder pattern for XML elements - XML 元素的流式构建器模式
- Attribute, child element, text, CDATA, comment support - 属性、子元素、文本、CDATA、注释支持
- Nested element building with startChild/endChild - 通过 startChild/endChild 构建嵌套元素
- Namespace-aware element creation - 支持命名空间的元素创建
- Consumer-based child configuration - 基于 Consumer 的子元素配置
Usage Examples | 使用示例:
// Build a simple element
XmlElement element = ElementBuilder.create("user")
.attribute("id", "1")
.child("name", "John Doe")
.child("email", "john@example.com")
.build();
// Build nested elements
XmlElement element = ElementBuilder.create("user")
.attribute("id", "1")
.startChild("address")
.child("city", "New York")
.child("country", "USA")
.endChild()
.build();
Security | 安全性:
- Thread-safe: No (mutable builder state) - 线程安全: 否(可变构建器状态)
- Null-safe: Partial (null values are skipped for attributes and text) - 空值安全: 部分(属性和文本的空值被跳过)
Performance | 性能特性:
- Time complexity: O(n) overall where n=number of children/attributes added; each individual operation is O(1) - 时间复杂度: 总体 O(n),n 为添加的子元素/属性数量;每次单独操作为 O(1)
- Space complexity: O(n) for the DOM subtree built; no auxiliary data structures beyond the DOM itself - 空间复杂度: 构建的 DOM 子树为 O(n);除 DOM 自身外无额外数据结构
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionAdds an attribute with boolean value.Adds an attribute with number value.Adds an attribute to the element.Adds an attribute with namespace.attributeIfNotNull(String name, String value) Adds an attribute if value is not null.build()Builds and returns the XmlElement.Adds CDATA content.child(XmlElement xmlElement) Adds a built XmlElement as a child.Adds a child element with boolean content.Adds a child element with number content.Adds a child element with text content.child(String name, Consumer<ElementBuilder> configurer) Configures a child element using a consumer.childIfNotNull(String name, String text) Adds a child element if value is not null.Adds a comment.static ElementBuilderCreates a new element builder with the given name.static ElementBuilderCreates a new element builder with namespace.defaultNamespace(String namespaceURI) Sets the default namespace.emptyChild(String name) Adds an empty child element.endChild()Ends building the current child and returns to the parent.Adds a namespace declaration.startChild(String name) Starts building a child element.startChild(String namespaceURI, String name) Starts building a child element with namespace.Sets the text content of the element.toXml()Converts the element to an XML string.toXml(int indent) Converts the element to a formatted XML string.unwrap()Gets the underlying DOM Element.
-
Method Details
-
create
Creates a new element builder with the given name. 使用给定名称创建新的元素构建器。- Parameters:
name- the element name | 元素名称- Returns:
- a new builder | 新构建器
-
create
Creates a new element builder with namespace. 使用命名空间创建新的元素构建器。- Parameters:
namespaceURI- the namespace URI | 命名空间 URIname- the element name | 元素名称- Returns:
- a new builder | 新构建器
-
attribute
Adds an attribute to the element. 向元素添加属性。- Parameters:
name- the attribute name | 属性名称value- the attribute value | 属性值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
attribute
Adds an attribute with namespace. 添加带命名空间的属性。- Parameters:
namespaceURI- the namespace URI | 命名空间 URIname- the attribute name | 属性名称value- the attribute value | 属性值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
attribute
Adds an attribute with number value. 添加带数字值的属性。- Parameters:
name- the attribute name | 属性名称value- the number value | 数字值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
attribute
Adds an attribute with boolean value. 添加带布尔值的属性。- Parameters:
name- the attribute name | 属性名称value- the boolean value | 布尔值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
attributeIfNotNull
Adds an attribute if value is not null. 如果值不为 null 则添加属性。- Parameters:
name- the attribute name | 属性名称value- the attribute value | 属性值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
namespace
Adds a namespace declaration. 添加命名空间声明。- Parameters:
namespaceURI- the namespace URI | 命名空间 URIprefix- the prefix | 前缀- Returns:
- this builder for chaining | 此构建器以便链式调用
-
defaultNamespace
Sets the default namespace. 设置默认命名空间。- Parameters:
namespaceURI- the namespace URI | 命名空间 URI- Returns:
- this builder for chaining | 此构建器以便链式调用
-
child
Adds a child element with text content. 添加带文本内容的子元素。- Parameters:
name- the element name | 元素名称text- the text content | 文本内容- Returns:
- this builder for chaining | 此构建器以便链式调用
-
child
Adds a child element with number content. 添加带数字内容的子元素。- Parameters:
name- the element name | 元素名称value- the number value | 数字值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
child
Adds a child element with boolean content. 添加带布尔内容的子元素。- Parameters:
name- the element name | 元素名称value- the boolean value | 布尔值- Returns:
- this builder for chaining | 此构建器以便链式调用
-
childIfNotNull
Adds a child element if value is not null. 如果值不为 null 则添加子元素。- Parameters:
name- the element name | 元素名称text- the text content | 文本内容- Returns:
- this builder for chaining | 此构建器以便链式调用
-
emptyChild
Adds an empty child element. 添加空子元素。- Parameters:
name- the element name | 元素名称- Returns:
- this builder for chaining | 此构建器以便链式调用
-
startChild
Starts building a child element. 开始构建子元素。- Parameters:
name- the element name | 元素名称- Returns:
- a new builder for the child | 子元素的新构建器
-
startChild
Starts building a child element with namespace. 开始构建带命名空间的子元素。- Parameters:
namespaceURI- the namespace URI | 命名空间 URIname- the element name | 元素名称- Returns:
- a new builder for the child | 子元素的新构建器
-
endChild
Ends building the current child and returns to the parent. 结束构建当前子元素并返回到父元素。- Returns:
- the parent builder | 父构建器
-
child
Adds a built XmlElement as a child. 添加已构建的 XmlElement 作为子元素。- Parameters:
xmlElement- the element to add | 要添加的元素- Returns:
- this builder for chaining | 此构建器以便链式调用
-
child
Configures a child element using a consumer. 使用消费者配置子元素。- Parameters:
name- the element name | 元素名称configurer- the element configurer | 元素配置器- Returns:
- this builder for chaining | 此构建器以便链式调用
-
text
Sets the text content of the element. 设置元素的文本内容。- Parameters:
text- the text content | 文本内容- Returns:
- this builder for chaining | 此构建器以便链式调用
-
cdata
Adds CDATA content. 添加 CDATA 内容。- Parameters:
data- the CDATA content | CDATA 内容- Returns:
- this builder for chaining | 此构建器以便链式调用
-
comment
Adds a comment. 添加注释。- Parameters:
comment- the comment text | 注释文本- Returns:
- this builder for chaining | 此构建器以便链式调用
-
build
Builds and returns the XmlElement. 构建并返回 XmlElement。- Returns:
- the built element | 构建的元素
-
unwrap
Gets the underlying DOM Element. 获取底层 DOM 元素。- Returns:
- the DOM Element | DOM 元素
-
toXml
Converts the element to an XML string. 将元素转换为 XML 字符串。- Returns:
- the XML string | XML 字符串
-
toXml
Converts the element to a formatted XML string. 将元素转换为格式化的 XML 字符串。- Parameters:
indent- the indent spaces | 缩进空格数- Returns:
- the XML string | XML 字符串
-