Class XmlTransformer
java.lang.Object
cloud.opencode.base.xml.transform.XmlTransformer
XML Transformer - XML serialization and formatting utilities
XML 转换器 - XML 序列化和格式化工具
This class provides utilities for XML serialization, formatting, and identity transformation.
此类提供 XML 序列化、格式化和恒等转换的工具。
Usage Examples | 使用示例:
// Format XML
String formatted = XmlTransformer.format(xml, 4);
// Minify XML
String minified = XmlTransformer.minify(xml);
// Serialize document to file
XmlTransformer.serialize(document, Path.of("output.xml"), 4);
// Copy/clone an element
XmlElement clone = XmlTransformer.clone(element);
Features | 主要功能:
- XML serialization and formatting - XML 序列化和格式化
- Minification and pretty-printing - 压缩和美化输出
- Element cloning and identity transformation - 元素克隆和恒等转换
Security | 安全性:
- Thread-safe: Yes (stateless utility, secure factory) - 线程安全: 是(无状态工具,安全工厂)
- Null-safe: No (throws on null input) - 空值安全: 否(null 输入抛异常)
Performance | 性能特性:
- Time complexity: O(n) for all format/minify/serialize operations where n=document or XML string size (full Transformer traversal) - 时间复杂度: 所有 format/minify/serialize 操作为 O(n),n 为文档或 XML 字符串大小(完整 Transformer 遍历)
- Space complexity: O(n) for output string or stream buffer - 空间复杂度: 输出字符串或流缓冲区为 O(n)
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringcanonicalize(String xml) Converts an XML string to a canonical form.static XmlDocumentclone(XmlDocument document) Clones an XmlDocument.static XmlElementclone(XmlElement element) Clones an XmlElement.static Stringformat(XmlDocument document, int indent) Formats an XmlDocument with indentation.static StringFormats an XML string with default indentation (4 spaces).static StringFormats an XML string with indentation.static Stringminify(XmlDocument document) Minifies an XmlDocument by removing whitespace.static StringMinifies an XML string by removing whitespace.static StringparseAndFormat(String xml, int indent) Parses and formats an XML string.static Stringserialize(XmlDocument document) Serializes an XmlDocument to a string.static Stringserialize(XmlDocument document, int indent) Serializes an XmlDocument to a formatted string.static voidserialize(XmlDocument document, OutputStream output) Serializes an XmlDocument to an output stream.static voidserialize(XmlDocument document, OutputStream output, int indent) Serializes an XmlDocument to an output stream with formatting.static voidserialize(XmlDocument document, Path path) Serializes an XmlDocument to a file.static voidserialize(XmlDocument document, Path path, int indent) Serializes an XmlDocument to a file with formatting.static voidserialize(XmlDocument document, Path path, int indent, String encoding) Serializes an XmlDocument to a file with options.static Stringserialize(XmlElement element) Serializes an XmlElement to a string.static Stringserialize(XmlElement element, int indent) Serializes an XmlElement to a formatted string.
-
Method Details
-
format
-
format
-
format
Formats an XmlDocument with indentation. 使用缩进格式化 XmlDocument。- Parameters:
document- the document | 文档indent- the indent spaces | 缩进空格数- Returns:
- the formatted XML | 格式化的 XML
-
minify
-
minify
Minifies an XmlDocument by removing whitespace. 通过删除空白字符来压缩 XmlDocument。- Parameters:
document- the document | 文档- Returns:
- the minified XML | 压缩的 XML
-
serialize
Serializes an XmlDocument to a string. 将 XmlDocument 序列化为字符串。- Parameters:
document- the document | 文档- Returns:
- the XML string | XML 字符串
-
serialize
Serializes an XmlDocument to a formatted string. 将 XmlDocument 序列化为格式化的字符串。- Parameters:
document- the document | 文档indent- the indent spaces | 缩进空格数- Returns:
- the XML string | XML 字符串
-
serialize
Serializes an XmlDocument to a file. 将 XmlDocument 序列化到文件。- Parameters:
document- the document | 文档path- the output path | 输出路径
-
serialize
Serializes an XmlDocument to a file with formatting. 将 XmlDocument 序列化到文件并格式化。- Parameters:
document- the document | 文档path- the output path | 输出路径indent- the indent spaces | 缩进空格数
-
serialize
Serializes an XmlDocument to a file with options. 使用选项将 XmlDocument 序列化到文件。- Parameters:
document- the document | 文档path- the output path | 输出路径indent- the indent spaces | 缩进空格数encoding- the encoding | 编码
-
serialize
Serializes an XmlDocument to an output stream. 将 XmlDocument 序列化到输出流。- Parameters:
document- the document | 文档output- the output stream | 输出流
-
serialize
Serializes an XmlDocument to an output stream with formatting. 将 XmlDocument 序列化到输出流并格式化。- Parameters:
document- the document | 文档output- the output stream | 输出流indent- the indent spaces | 缩进空格数
-
serialize
Serializes an XmlElement to a string. 将 XmlElement 序列化为字符串。- Parameters:
element- the element | 元素- Returns:
- the XML string | XML 字符串
-
serialize
Serializes an XmlElement to a formatted string. 将 XmlElement 序列化为格式化的字符串。- Parameters:
element- the element | 元素indent- the indent spaces | 缩进空格数- Returns:
- the XML string | XML 字符串
-
clone
Clones an XmlDocument. 克隆 XmlDocument。- Parameters:
document- the document to clone | 要克隆的文档- Returns:
- the cloned document | 克隆的文档
-
clone
Clones an XmlElement. 克隆 XmlElement。- Parameters:
element- the element to clone | 要克隆的元素- Returns:
- the cloned element | 克隆的元素
-
parseAndFormat
-
canonicalize
-