Xstream 简明教程

XStream - Overview

XStream 是一个简单的基于 Java 的库,用于将 Java 对象序列化为 XML,反之亦然。

XStream is a simple Java-based library to serialize Java objects to XML and vice versa.

Features

  1. Easy to use − XStream API provides a high-level facade to simplify common use cases.

  2. No need to create mapping − XStream API provides default mapping for most of the objects to be serialized.

  3. Performance − XStream is fast and is of low memory footprint, which is suitable for large object graphs or systems.

  4. Clean XML − XStream produces clean and compact XML output that is easy to read.

  5. Object modification not required − XStream serializes internal fields like private and final fields, and supports non-public and inner classes. Default constructor is not a mandatory requirement.

  6. Full object graph support − XStream allows to maintain duplicate references encountered in the object-model and also supports circular references.

  7. Customizable conversion strategies − Custom strategies can be registered in order to allow customization of a particular type to be represented as XML.

  8. Security framework − XStream provides a fair control over unmarshalled types to prevent security issues with manipulated input.

  9. Error messages − When an exception occurs due to malformed XML, it provides detailed diagnostics to fix the problem.

  10. Alternative output format − XStream supports other output formats like JSON and morphing.

Common Uses

  1. Transport − XML is a text representation of object and can be used to transport objects over the wire independent of the serialization / deserialization techniques used.

  2. Persistence − Objects can be persisted as XML in databases and can be marshalled/unmarshalled as and when required.

  3. Configuration − XML is self-explanatory and is heavily used to define configurations. Objects can also be used for configuration purpose after converting them to XML representation.

  4. Unit Tests − XStream API is JUnit compatible and can be used to enhance unit testing of application modules.