Class XmlBinder
java.lang.Object
cloud.opencode.base.xml.bind.XmlBinder
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 Summary
Modifier and TypeMethodDescriptionstatic XmlBindercreate()Creates a new XML binder.Sets the output encoding.formatted(boolean formatted) Sets formatted output.indent(int indent) Sets the indentation level.Marshals object to XML string.Marshals object to formatted XML string.marshalToDocument(Object obj) Marshals object to XML document.registerAdapter(XmlAdapter<?, ?> adapter) Registers a type adapter.Sets schema validation.<T> Tunmarshal(XmlDocument document, Class<T> clazz) Unmarshals XML document to object.<T> Tunmarshal(XmlElement element, Class<T> clazz) Unmarshals XML element to object.<T> TUnmarshals XML string to object.
-
Method Details
-
create
Creates a new XML binder. 创建新的 XML 绑定器。- Returns:
- a new binder | 新绑定器
-
registerAdapter
Registers a type adapter. 注册类型适配器。- Parameters:
adapter- the adapter | 适配器- Returns:
- this binder for chaining | 此绑定器以便链式调用
-
formatted
Sets formatted output. 设置格式化输出。- Parameters:
formatted- whether to format | 是否格式化- Returns:
- this binder for chaining | 此绑定器以便链式调用
-
encoding
-
indent
Sets the indentation level. 设置缩进级别。- Parameters:
indent- the indent spaces | 缩进空格数- Returns:
- this binder for chaining | 此绑定器以便链式调用
-
schema
-
unmarshal
-
unmarshal
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
Unmarshals XML element to object. 将 XML 元素解组为对象。- Type Parameters:
T- the type parameter | 类型参数- Parameters:
element- the XML element | XML 元素clazz- the target type | 目标类型- Returns:
- the object | 对象
-
marshal
-
marshal
-
marshalToDocument
Marshals object to XML document. 将对象编组为 XML 文档。- Parameters:
obj- the object | 对象- Returns:
- the XML document | XML 文档
-