Class XmlTransformer

java.lang.Object
cloud.opencode.base.xml.transform.XmlTransformer

public final class XmlTransformer extends Object
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 Details

    • format

      public static String format(String xml, int indent)
      Formats an XML string with indentation. 使用缩进格式化 XML 字符串。
      Parameters:
      xml - the XML string | XML 字符串
      indent - the indent spaces | 缩进空格数
      Returns:
      the formatted XML | 格式化的 XML
    • format

      public static String format(String xml)
      Formats an XML string with default indentation (4 spaces). 使用默认缩进(4 个空格)格式化 XML 字符串。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      the formatted XML | 格式化的 XML
    • format

      public static String format(XmlDocument document, int indent)
      Formats an XmlDocument with indentation. 使用缩进格式化 XmlDocument。
      Parameters:
      document - the document | 文档
      indent - the indent spaces | 缩进空格数
      Returns:
      the formatted XML | 格式化的 XML
    • minify

      public static String minify(String xml)
      Minifies an XML string by removing whitespace. 通过删除空白字符来压缩 XML 字符串。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      the minified XML | 压缩的 XML
    • minify

      public static String minify(XmlDocument document)
      Minifies an XmlDocument by removing whitespace. 通过删除空白字符来压缩 XmlDocument。
      Parameters:
      document - the document | 文档
      Returns:
      the minified XML | 压缩的 XML
    • serialize

      public static String serialize(XmlDocument document)
      Serializes an XmlDocument to a string. 将 XmlDocument 序列化为字符串。
      Parameters:
      document - the document | 文档
      Returns:
      the XML string | XML 字符串
    • serialize

      public static String serialize(XmlDocument document, int indent)
      Serializes an XmlDocument to a formatted string. 将 XmlDocument 序列化为格式化的字符串。
      Parameters:
      document - the document | 文档
      indent - the indent spaces | 缩进空格数
      Returns:
      the XML string | XML 字符串
    • serialize

      public static void serialize(XmlDocument document, Path path)
      Serializes an XmlDocument to a file. 将 XmlDocument 序列化到文件。
      Parameters:
      document - the document | 文档
      path - the output path | 输出路径
    • serialize

      public static void serialize(XmlDocument document, Path path, int indent)
      Serializes an XmlDocument to a file with formatting. 将 XmlDocument 序列化到文件并格式化。
      Parameters:
      document - the document | 文档
      path - the output path | 输出路径
      indent - the indent spaces | 缩进空格数
    • serialize

      public static void serialize(XmlDocument document, Path path, int indent, String encoding)
      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

      public static void serialize(XmlDocument document, OutputStream output)
      Serializes an XmlDocument to an output stream. 将 XmlDocument 序列化到输出流。
      Parameters:
      document - the document | 文档
      output - the output stream | 输出流
    • serialize

      public static void serialize(XmlDocument document, OutputStream output, int indent)
      Serializes an XmlDocument to an output stream with formatting. 将 XmlDocument 序列化到输出流并格式化。
      Parameters:
      document - the document | 文档
      output - the output stream | 输出流
      indent - the indent spaces | 缩进空格数
    • serialize

      public static String serialize(XmlElement element)
      Serializes an XmlElement to a string. 将 XmlElement 序列化为字符串。
      Parameters:
      element - the element | 元素
      Returns:
      the XML string | XML 字符串
    • serialize

      public static String serialize(XmlElement element, int indent)
      Serializes an XmlElement to a formatted string. 将 XmlElement 序列化为格式化的字符串。
      Parameters:
      element - the element | 元素
      indent - the indent spaces | 缩进空格数
      Returns:
      the XML string | XML 字符串
    • clone

      public static XmlDocument clone(XmlDocument document)
      Clones an XmlDocument. 克隆 XmlDocument。
      Parameters:
      document - the document to clone | 要克隆的文档
      Returns:
      the cloned document | 克隆的文档
    • clone

      public static XmlElement clone(XmlElement element)
      Clones an XmlElement. 克隆 XmlElement。
      Parameters:
      element - the element to clone | 要克隆的元素
      Returns:
      the cloned element | 克隆的元素
    • parseAndFormat

      public static String parseAndFormat(String xml, int indent)
      Parses and formats an XML string. 解析并格式化 XML 字符串。
      Parameters:
      xml - the XML string | XML 字符串
      indent - the indent spaces | 缩进空格数
      Returns:
      the formatted XML | 格式化的 XML
    • canonicalize

      public static String canonicalize(String xml)
      Converts an XML string to a canonical form. 将 XML 字符串转换为规范形式。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      the canonical XML | 规范化的 XML