Class StaxWriter
java.lang.Object
cloud.opencode.base.xml.stax.StaxWriter
- All Implemented Interfaces:
Closeable, AutoCloseable
StAX Writer - Streaming XML writer
StAX 写入器 - 流式 XML 写入器
This class provides a fluent API for streaming XML output using StAX. StAX writing is memory-efficient for generating large XML files.
此类提供使用 StAX 进行流式 XML 输出的流式 API。 StAX 写入对于生成大型 XML 文件内存效率高。
Usage Examples | 使用示例:
// Write to string
String xml = StaxWriter.create()
.startDocument()
.startElement("users")
.startElement("user")
.attribute("id", "1")
.element("name", "John")
.element("email", "john@example.com")
.endElement()
.endElement()
.endDocument()
.toString();
// Write to file
StaxWriter.create(Path.of("users.xml"))
.startDocument("UTF-8", "1.0")
.startElement("users")
// ... add content
.endDocument()
.close();
Features | 主要功能:
- Fluent API for streaming XML output - 用于流式 XML 输出的流式 API
- Memory-efficient writing for large XML files - 大型 XML 文件的内存高效写入
- Automatic indentation and formatting - 自动缩进和格式化
- Namespace and attribute support - 命名空间和属性支持
Security | 安全性:
- Thread-safe: No (not designed for shared use) - 线程安全: 否(不适用于共享使用)
- Null-safe: No (throws on null element names) - 空值安全: 否(null 元素名称抛异常)
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionWrites an attribute with boolean value.Writes an attribute with number value.Writes an attribute.Writes an attribute with namespace.Writes an attribute with prefix and namespace.attributeIfNotNull(String localName, String value) Writes an attribute if value is not null.Writes CDATA content.voidclose()Writes a comment.static StaxWritercreate()Creates a new writer that writes to a string.static StaxWritercreate(OutputStream output) Creates a new writer that writes to an output stream.static StaxWriterCreates a new writer that writes to a file.defaultNamespace(String namespaceURI) Writes a default namespace declaration.Writes a complete element with boolean content.Writes a complete element with number content.Writes a complete element with text content.elementIfNotNull(String localName, String text) Writes a complete element with text content if not null.emptyElement(String localName) Writes an empty element.Ends the document.Ends the current element.flush()Flushes the writer.formatted(boolean formatted) Enables formatted output with indentation.Gets the underlying XMLStreamWriter.indent(int spaces) Sets the indent size in spaces.Sets the indent string.Writes a namespace declaration.processingInstruction(String target, String data) Writes a processing instruction.Writes the XML declaration.startDocument(String encoding, String version) Writes the XML declaration with encoding and version.startElement(String localName) Starts an element.startElement(String namespaceURI, String localName) Starts an element with namespace.startElement(String prefix, String localName, String namespaceURI) Starts an element with prefix and namespace.Writes text content.toString()
-
Method Details
-
create
Creates a new writer that writes to a string. 创建写入字符串的新写入器。- Returns:
- a new writer | 新写入器
-
create
Creates a new writer that writes to an output stream. 创建写入输出流的新写入器。- Parameters:
output- the output stream | 输出流- Returns:
- a new writer | 新写入器
-
create
Creates a new writer that writes to a file. 创建写入文件的新写入器。- Parameters:
path- the file path | 文件路径- Returns:
- a new writer | 新写入器
-
formatted
Enables formatted output with indentation. 启用带缩进的格式化输出。- Parameters:
formatted- whether to format | 是否格式化- Returns:
- this writer for chaining | 此写入器以便链式调用
-
indent
Sets the indent string. 设置缩进字符串。- Parameters:
indent- the indent string | 缩进字符串- Returns:
- this writer for chaining | 此写入器以便链式调用
-
indent
Sets the indent size in spaces. 设置空格缩进大小。- Parameters:
spaces- the number of spaces | 空格数- Returns:
- this writer for chaining | 此写入器以便链式调用
-
startDocument
Writes the XML declaration. 写入 XML 声明。- Returns:
- this writer for chaining | 此写入器以便链式调用
-
startDocument
Writes the XML declaration with encoding and version. 写入带编码和版本的 XML 声明。- Parameters:
encoding- the encoding | 编码version- the XML version | XML 版本- Returns:
- this writer for chaining | 此写入器以便链式调用
-
endDocument
Ends the document. 结束文档。- Returns:
- this writer for chaining | 此写入器以便链式调用
-
startElement
Starts an element. 开始一个元素。- Parameters:
localName- the element name | 元素名称- Returns:
- this writer for chaining | 此写入器以便链式调用
-
startElement
Starts an element with namespace. 开始带命名空间的元素。- Parameters:
namespaceURI- the namespace URI | 命名空间 URIlocalName- the element name | 元素名称- Returns:
- this writer for chaining | 此写入器以便链式调用
-
startElement
Starts an element with prefix and namespace. 开始带前缀和命名空间的元素。- Parameters:
prefix- the prefix | 前缀localName- the element name | 元素名称namespaceURI- the namespace URI | 命名空间 URI- Returns:
- this writer for chaining | 此写入器以便链式调用
-
endElement
Ends the current element. 结束当前元素。- Returns:
- this writer for chaining | 此写入器以便链式调用
-
emptyElement
Writes an empty element. 写入空元素。- Parameters:
localName- the element name | 元素名称- Returns:
- this writer for chaining | 此写入器以便链式调用
-
element
Writes a complete element with text content. 写入带文本内容的完整元素。- Parameters:
localName- the element name | 元素名称text- the text content | 文本内容- Returns:
- this writer for chaining | 此写入器以便链式调用
-
elementIfNotNull
Writes a complete element with text content if not null. 如果不为 null 则写入带文本内容的完整元素。- Parameters:
localName- the element name | 元素名称text- the text content | 文本内容- Returns:
- this writer for chaining | 此写入器以便链式调用
-
element
Writes a complete element with number content. 写入带数字内容的完整元素。- Parameters:
localName- the element name | 元素名称value- the number value | 数字值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
element
Writes a complete element with boolean content. 写入带布尔内容的完整元素。- Parameters:
localName- the element name | 元素名称value- the boolean value | 布尔值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
attribute
Writes an attribute. 写入属性。- Parameters:
localName- the attribute name | 属性名称value- the attribute value | 属性值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
attribute
Writes an attribute with namespace. 写入带命名空间的属性。- Parameters:
namespaceURI- the namespace URI | 命名空间 URIlocalName- the attribute name | 属性名称value- the attribute value | 属性值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
attribute
Writes an attribute with prefix and namespace. 写入带前缀和命名空间的属性。- Parameters:
prefix- the prefix | 前缀namespaceURI- the namespace URI | 命名空间 URIlocalName- the attribute name | 属性名称value- the attribute value | 属性值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
attributeIfNotNull
Writes an attribute if value is not null. 如果值不为 null 则写入属性。- Parameters:
localName- the attribute name | 属性名称value- the attribute value | 属性值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
attribute
Writes an attribute with number value. 写入带数字值的属性。- Parameters:
localName- the attribute name | 属性名称value- the number value | 数字值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
attribute
Writes an attribute with boolean value. 写入带布尔值的属性。- Parameters:
localName- the attribute name | 属性名称value- the boolean value | 布尔值- Returns:
- this writer for chaining | 此写入器以便链式调用
-
text
Writes text content. 写入文本内容。- Parameters:
text- the text | 文本- Returns:
- this writer for chaining | 此写入器以便链式调用
-
cdata
Writes CDATA content. 写入 CDATA 内容。- Parameters:
data- the CDATA content | CDATA 内容- Returns:
- this writer for chaining | 此写入器以便链式调用
-
comment
Writes a comment. 写入注释。- Parameters:
comment- the comment text | 注释文本- Returns:
- this writer for chaining | 此写入器以便链式调用
-
processingInstruction
Writes a processing instruction. 写入处理指令。- Parameters:
target- the target | 目标data- the data | 数据- Returns:
- this writer for chaining | 此写入器以便链式调用
-
defaultNamespace
Writes a default namespace declaration. 写入默认命名空间声明。- Parameters:
namespaceURI- the namespace URI | 命名空间 URI- Returns:
- this writer for chaining | 此写入器以便链式调用
-
namespace
Writes a namespace declaration. 写入命名空间声明。- Parameters:
prefix- the prefix | 前缀namespaceURI- the namespace URI | 命名空间 URI- Returns:
- this writer for chaining | 此写入器以便链式调用
-
flush
-
getUnderlyingWriter
Gets the underlying XMLStreamWriter. 获取底层 XMLStreamWriter。- Returns:
- the underlying writer | 底层写入器
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
toString
-