Class XmlBinder

java.lang.Object
cloud.opencode.base.xml.bind.XmlBinder

public final class XmlBinder extends Object
XML Binder - Binds XML to/from Java objects XML 绑定器 - 将 XML 绑定到 Java 对象或从 Java 对象绑定

This class provides methods for marshalling Java objects to XML and unmarshalling XML to Java objects using annotations.

此类提供使用注解将 Java 对象编组为 XML 以及将 XML 解组为 Java 对象的方法。

Features | 主要功能:

  • XML to Java object unmarshalling - XML 到 Java 对象解组
  • Java object to XML marshalling - Java 对象到 XML 编组
  • Custom type adapter registration - 自定义类型适配器注册
  • Annotation-driven field mapping - 注解驱动的字段映射
  • Formatted output with configurable indentation - 可配置缩进的格式化输出
  • Nested object and collection support - 嵌套对象和集合支持

Usage Examples | 使用示例:

// Unmarshal XML to object
User user = XmlBinder.create()
    .unmarshal(xml, User.class);

// Marshal object to XML
String xml = XmlBinder.create()
    .formatted(true)
    .marshal(user);

// With custom adapter
XmlBinder binder = XmlBinder.create()
    .registerAdapter(new LocalDateAdapter());

Security | 安全性:

  • Thread-safe: No (mutable configuration state) - 线程安全: 否(可变配置状态)
  • Null-safe: No (null inputs throw exceptions) - 空值安全: 否(空值输入抛出异常)
  • Uses reflection with setAccessible - 使用反射和 setAccessible
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static XmlBinder create()
      Creates a new XML binder. 创建新的 XML 绑定器。
      Returns:
      a new binder | 新绑定器
    • registerAdapter

      public XmlBinder registerAdapter(XmlAdapter<?,?> adapter)
      Registers a type adapter. 注册类型适配器。
      Parameters:
      adapter - the adapter | 适配器
      Returns:
      this binder for chaining | 此绑定器以便链式调用
    • formatted

      public XmlBinder formatted(boolean formatted)
      Sets formatted output. 设置格式化输出。
      Parameters:
      formatted - whether to format | 是否格式化
      Returns:
      this binder for chaining | 此绑定器以便链式调用
    • encoding

      public XmlBinder encoding(String encoding)
      Sets the output encoding. 设置输出编码。
      Parameters:
      encoding - the encoding | 编码
      Returns:
      this binder for chaining | 此绑定器以便链式调用
    • indent

      public XmlBinder indent(int indent)
      Sets the indentation level. 设置缩进级别。
      Parameters:
      indent - the indent spaces | 缩进空格数
      Returns:
      this binder for chaining | 此绑定器以便链式调用
    • schema

      public XmlBinder schema(Path schemaPath)
      Sets schema validation. 设置 Schema 验证。
      Parameters:
      schemaPath - the schema path | Schema 路径
      Returns:
      this binder for chaining | 此绑定器以便链式调用
    • unmarshal

      public <T> T unmarshal(String xml, Class<T> clazz)
      Unmarshals XML string to object. 将 XML 字符串解组为对象。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      xml - the XML string | XML 字符串
      clazz - the target type | 目标类型
      Returns:
      the object | 对象
    • unmarshal

      public <T> T unmarshal(XmlDocument document, Class<T> clazz)
      Unmarshals XML document to object. 将 XML 文档解组为对象。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      document - the XML document | XML 文档
      clazz - the target type | 目标类型
      Returns:
      the object | 对象
    • unmarshal

      public <T> T unmarshal(XmlElement element, Class<T> clazz)
      Unmarshals XML element to object. 将 XML 元素解组为对象。

      Supports both regular classes (via no-arg constructor) and records (via canonical constructor with component values from XML).

      支持普通类(通过无参构造函数)和记录(通过从 XML 获取组件值的规范构造函数)。

      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      element - the XML element | XML 元素
      clazz - the target type | 目标类型
      Returns:
      the object | 对象
    • marshal

      public String marshal(Object obj)
      Marshals object to XML string. 将对象编组为 XML 字符串。
      Parameters:
      obj - the object | 对象
      Returns:
      the XML string | XML 字符串
    • marshal

      public String marshal(Object obj, int indent)
      Marshals object to formatted XML string. 将对象编组为格式化的 XML 字符串。
      Parameters:
      obj - the object | 对象
      indent - the indentation | 缩进
      Returns:
      the XML string | XML 字符串
    • marshalToDocument

      public XmlDocument marshalToDocument(Object obj)
      Marshals object to XML document. 将对象编组为 XML 文档。
      Parameters:
      obj - the object | 对象
      Returns:
      the XML document | XML 文档