Interface XmlAdapter<V,B>

Type Parameters:
V - the XML value type (usually String) | XML 值类型(通常是 String)
B - the bound Java type | 绑定的 Java 类型
All Known Implementing Classes:
DateAdapter.DurationAdapter, DateAdapter.InstantAdapter, DateAdapter.LegacyDateAdapter, DateAdapter.LocalDateAdapter, DateAdapter.LocalDateTimeAdapter, DateAdapter.LocalTimeAdapter, DateAdapter.PeriodAdapter, DateAdapter.ZonedDateTimeAdapter

public interface XmlAdapter<V,B>
XML Adapter Interface - Converts between XML representation and Java types XML 适配器接口 - 在 XML 表示和 Java 类型之间转换

This interface defines methods for marshalling and unmarshalling custom types that cannot be directly represented in XML.

此接口定义用于编组和解组无法直接在 XML 中表示的自定义类型的方法。

Features | 主要功能:

  • Bidirectional type conversion (marshal/unmarshal) - 双向类型转换(编组/解组)
  • Pluggable adapter registration with XmlBinder - 可插拔适配器注册到 XmlBinder
  • Optional type introspection via getBoundType/getValueType - 通过 getBoundType/getValueType 进行可选类型内省

Usage Examples | 使用示例:

public class LocalDateAdapter implements XmlAdapter<String, LocalDate> {
    @Override
    public LocalDate unmarshal(String value) {
        return LocalDate.parse(value);
    }

    @Override
    public String marshal(LocalDate value) {
        return value.toString();
    }
}

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Depends on implementation - 空值安全: 取决于实现
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default Class<B>
    Returns the bound Java type.
    default Class<V>
    Returns the XML value type.
    marshal(B value)
    Converts a Java object to XML value.
    unmarshal(V value)
    Converts an XML value to a Java object.
  • Method Details

    • unmarshal

      B unmarshal(V value) throws Exception
      Converts an XML value to a Java object. 将 XML 值转换为 Java 对象。
      Parameters:
      value - the XML value | XML 值
      Returns:
      the Java object | Java 对象
      Throws:
      Exception - if conversion fails | 如果转换失败则抛出异常
    • marshal

      V marshal(B value) throws Exception
      Converts a Java object to XML value. 将 Java 对象转换为 XML 值。
      Parameters:
      value - the Java object | Java 对象
      Returns:
      the XML value | XML 值
      Throws:
      Exception - if conversion fails | 如果转换失败则抛出异常
    • getBoundType

      default Class<B> getBoundType()
      Returns the bound Java type. 返回绑定的 Java 类型。
      Returns:
      the bound type | 绑定类型
    • getValueType

      default Class<V> getValueType()
      Returns the XML value type. 返回 XML 值类型。
      Returns:
      the value type | 值类型