XStream xstream = new XStream(); String xml = xstream.toXML(myObject); // serialize to XML Object myObject2 = xstream.fromXML(xml); // deserialize from XML
To create shorter XML, you can specify aliases for classes using the alias() method. For example, you
can shorten all occurrences of element <com.blah.MyThing> to <my-thing> by
registering an alias for the class.
xstream.alias("my-thing", MyThing.class);
XStream contains a map of com. instances, each of which acts as a
strategy for converting a particular type of class to XML and back again. Out of the box, XStream contains converters
for most basic types (String, Date, int, boolean, etc) and collections (Map, List, Set, Properties, etc). For other
objects reflection is used to serialize each field recursively.
Extra converters can be registered using the registerConverter() method. Some non-standard converters
are supplied in the com. package and you can create your own by
implementing the com. interface.
xstream.registerConverter(new SqlTimestampConverter()); xstream.registerConverter(new DynamicProxyConverter());
The converters can be registered with an explicit priority. By default they are registered with
XStream.PRIORITY_NORMAL. Converters of same priority will be used in the reverse sequence they have been registered.
The default converter, i.e. the converter which will be used if no other registered converter is suitable, can be
registered with priority XStream.PRIORITY_VERY_LOW. XStream uses by default the
com. as the fallback converter.
xstream.registerConverter(new CustomDefaultConverter(), XStream.PRIORITY_VERY_LOW);
XStream has support for object graphs; a deserialized object graph will keep references intact, including circular references.
XStream can signify references in XML using either relative/absolute XPath or IDs. The mode can be changed using
setMode():
xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES); |
(Default) Uses XPath relative references to signify duplicate references. This produces XML with the least clutter. |
xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES); |
Uses XPath absolute references to signify duplicate references. This produces XML with the least clutter. |
xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES); |
Uses XPath relative references to signify duplicate references. The XPath expression ensures that a single node only is selected always. |
xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES); |
Uses XPath absolute references to signify duplicate references. The XPath expression ensures that a single node only is selected always. |
xstream.setMode(XStream.ID_REFERENCES); |
Uses ID references to signify duplicate references. In some scenarios, such as when using hand-written XML, this is easier to work with. |
xstream.setMode(XStream.NO_REFERENCES); |
This disables object graph support and treats the object structure like a tree. Duplicate references are treated as two separate objects and circular references cause an exception. This is slightly faster and uses less memory than the other two modes. |
The XStream instance is thread-safe. That is, once the XStream instance has been created and configured, it may be shared across multiple threads allowing objects to be serialized/deserialized concurrently. Note, that this only applies if annotations are not auto-detected on-the-fly.
To avoid the need for special tags for collections, you can define implicit collections using one of the
addImplicitCollection methods.
| Modifier and Type | Class and Description |
|---|---|
| public static class | XStream.InitializationException
Deprecated
As of 1.3, use
com. instead
|
| Modifier and Type | Field and Description |
|---|---|
| private static final String | |
| private AnnotationConfiguration | annotationConfiguration
Deprecated
as a consequence of
AnnotationConfiguration being deprecated.
|
| private AttributeAliasingMapper | |
| private AttributeMapper | |
| private ClassAliasingMapper | |
| private ClassLoaderReference | |
| public static final String | |
| public static final String | |
| private int | |
| private ConverterLookup | |
| private ConverterRegistry | |
| private DefaultImplementationsMapper | |
| private ElementIgnoringMapper | |
| private FieldAliasingMapper | |
| private HierarchicalStreamDriver | |
| public static final int | |
| private static final Pattern | |
| private ImmutableTypesMapper | |
| private ImplicitCollectionMapper | |
| private LocalConversionMapper | |
| private Mapper | |
| private MarshallingStrategy | |
| public static final int | |
| private PackageAliasingMapper | |
| public static final int | |
| public static final int | |
| public static final int | |
| public static final int | |
| private ReflectionProvider | |
| private SecurityMapper | |
| public static final int | |
| public static final int | |
| private SystemAttributeAliasingMapper | |
| public static final int | |
| public static final int |
| Access | Constructor and Description |
|---|---|
| public | |
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching reflection provider reflectionProvider)Constructs an XStream with a special |
| public | XStream(HierarchicalStreamDriver
the driver instance hierarchicalStreamDriver)Constructs an XStream with a special |
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching Provider reflectionProvider, HierarchicalStreamDriver the driver instance hierarchicalStreamDriver)Constructs an XStream with a special |
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching Provider reflectionProvider, Mapper the instance with the mapper, HierarchicalStreamDriver Mapper chain or null for the default chainthe driver instance driver)
Deprecated
As of 1.3, use
Constructs an XStream with a special XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoader, Mapper)
instead
HierarchicalStreamDriver, ReflectionProvider and a prepared
Mapper chain.
|
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching Provider reflectionProvider, HierarchicalStreamDriver the driver instance driver, ClassLoaderReference the reference to the classLoaderReference)ClassLoader to useConstructs an XStream with a special |
| public | XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader)
Deprecated
As of 1.4.5 use
Constructs an XStream with a special XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference)
HierarchicalStreamDriver, ReflectionProvider and the
ClassLoader to use.
|
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching Provider reflectionProvider, HierarchicalStreamDriver the driver instance driver, ClassLoader the classLoader, Mapper ClassLoader to usethe instance with the mapper)Mapper chain or null for the default chain
Deprecated
As of 1.4.5 use
Constructs an XStream with a special XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference, Mapper)
HierarchicalStreamDriver, ReflectionProvider, a prepared
Mapper chain and the ClassLoader to use.
|
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching Provider reflectionProvider, HierarchicalStreamDriver the driver instance driver, ClassLoaderReference the reference to the classLoaderReference, Mapper ClassLoader to usethe instance with the mapper)Mapper chain or null for the default chainConstructs an XStream with a special |
| private | XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoader, Mapper mapper, final DefaultConverterLookup defaultConverterLookup)
|
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching Provider reflectionProvider, HierarchicalStreamDriver the driver instance driver, ClassLoader the classLoader, Mapper ClassLoader to usethe instance with the mapper, ConverterLookup Mapper chain or null for the default chainthe instance that is used to lookup the converters converterLookup, ConverterRegistry an instance to manage the converter instances converterRegistry)
Deprecated
As of 1.4.5 use
Constructs an XStream with a special XStream(ReflectionProvider, HierarchicalStreamDriver, ClassLoaderReference, Mapper, ConverterLookup, ConverterRegistry)
HierarchicalStreamDriver, ReflectionProvider, a prepared
Mapper chain, the ClassLoaderReference and an own ConverterLookup and
ConverterRegistry.
|
| public | XStream(ReflectionProvider
the reflection provider to use or null for best matching Provider reflectionProvider, HierarchicalStreamDriver the driver instance driver, ClassLoaderReference the reference to the classLoaderReference, Mapper ClassLoader to usethe instance with the mapper, ConverterLookup Mapper chain or null for the default chainthe instance that is used to lookup the converters converterLookup, ConverterRegistry an instance to manage the converter instances or null to prevent any further
registry (including annotations) converterRegistry)Constructs an XStream with a special |
| Modifier and Type | Method and Description |
|---|---|
| public void | addDefaultImplementation(Class<T> defaultImplementation, Class<T> ofType)
Associate a default implementation of a class with an object. |
| public void | addImmutableType(Class<T> type)
Deprecated
As of 1.4.9 use
Add immutable types.
addImmutableType(Class, boolean)
|
| public void | addImmutableType(final Class<T> type, final boolean
true if support at deserialization time is required for compatibility at the
cost of a higher memory footprint, false otherwiseAdd immutable types. |
| private void | |
| public void | addImplicitArray(Class<T>
class owning the implicit array ownerType, String name of the array field fieldName)Adds an implicit array. |
| public void | addImplicitArray(Class<T>
class owning the implicit array ownerType, String name of the array field in the ownerType fieldName, Class<T> type of the items to be part of this array itemType)Adds an implicit array which is used for all items of the given itemType when the array type matches. |
| public void | addImplicitArray(Class<T>
class owning the implicit array ownerType, String name of the array field in the ownerType fieldName, String alias name of the items itemName)Adds an implicit array which is used for all items of the given element name defined by itemName. |
| public void | addImplicitCollection(Class<T>
class owning the implicit collection ownerType, String name of the field in the ownerType. This field must be a concrete collection type or matching
the default implementation type of the collection type. fieldName)Adds a default implicit collection which is used for any unmapped XML tag. |
| public void | addImplicitCollection(Class<T>
class owning the implicit collection ownerType, String name of the field in the ownerType. This field must be a concrete collection type or matching
the default implementation type of the collection type. fieldName, Class<T> type of the items to be part of this collection itemType)Adds implicit collection which is used for all items of the given itemType. |
| public void | addImplicitCollection(Class<T>
class owning the implicit collection ownerType, String name of the field in the ownerType. This field must be a concrete collection type or matching
the default implementation type of the collection type. fieldName, String element name of the implicit collection itemFieldName, Class<T> item type to be aliases be the itemFieldName itemType)Adds implicit collection which is used for all items of the given element name defined by itemFieldName. |
| public void | addImplicitMap(Class<T>
class owning the implicit map ownerType, String name of the field in the ownerType. This field must be a concrete map type or matching the
default implementation type of the map type. fieldName, Class<T> type of the items to be part of this map as value itemType, String the name of the field of the itemType that is used for the key in the map keyFieldName)Adds an implicit map. |
| public void | addImplicitMap(Class<T>
class owning the implicit map ownerType, String name of the field in the ownerType. This field must be a concrete map type or matching the
default implementation type of the map type. fieldName, String alias name of the items itemName, Class<T> type of the items to be part of this map as value itemType, String the name of the field of the itemType that is used for the key in the map keyFieldName)Adds an implicit map. |
| public void | |
| public void | |
| public void | |
| public void | aliasAttribute(String
the alias itself alias, String the name of the attribute attributeName)Create an alias for an attribute |
| public void | aliasAttribute(Class<T>
the type where the attribute is defined definedIn, String the name of the attribute attributeName, String the alias itself alias)Create an alias for an attribute. |
| private void | |
| public void | aliasField(String
the alias itself alias, Class<T> the type that declares the field definedIn, String the name of the field fieldName)Create an alias for a field name. |
| public void | aliasPackage(String
Short name name, String package to be aliased pkgName)Alias a package to a shorter name to be used in XML elements. |
| public void | aliasSystemAttribute(String
the alias itself (may be alias, String null)the name of the system attribute systemAttributeName)Create an alias for a system attribute. |
| public void | |
| public void | allowTypeHierarchy(Class<T>
the base type to allow type)Add security permission for a type hierarchy. |
| public void | allowTypes(String[]
the type names to allow names)Add security permission for explicit types by name. |
| public void | |
| public void | allowTypesByRegExp(String[]
the regular expressions to allow type names regexps)Add security permission for types matching one of the specified regular expressions. |
| public void | allowTypesByRegExp(Pattern[]
the regular expressions to allow type names regexps)Add security permission for types matching one of the specified regular expressions. |
| public void | allowTypesByWildcard(String[]
the patterns to allow type names patterns)Add security permission for types matching one of the specified wildcard patterns. |
| public void | autodetectAnnotations(boolean
true if annotations are auto-detectedSet the auto-detection mode of the AnnotationMapper. |
| private Mapper | |
| private Mapper | buildMapperDynamically(String className, Class<T>[] constructorParamTypes, Object[] constructorParamValues)
|
| public ObjectInputStream | createObjectInputStream(Reader xmlReader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream. |
| public ObjectInputStream | createObjectInputStream(InputStream in)
Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using XStream. |
| public ObjectInputStream | createObjectInputStream(final HierarchicalStreamReader reader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream. |
| public ObjectInputStream | createObjectInputStream(final HierarchicalStreamReader reader, DataHolder dataHolder)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream. |
| public ObjectOutputStream | createObjectOutputStream(Writer writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. |
| public ObjectOutputStream | createObjectOutputStream(HierarchicalStreamWriter writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. |
| public ObjectOutputStream | createObjectOutputStream(Writer writer, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. |
| public ObjectOutputStream | createObjectOutputStream(OutputStream out)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream. |
| public ObjectOutputStream | createObjectOutputStream(OutputStream out, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream. |
| public ObjectOutputStream | createObjectOutputStream(final HierarchicalStreamWriter
The writer to serialize the objects to. writer, final String The name of the root node enclosing the stream of objects. rootNodeName)Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. |
| public ObjectOutputStream | createObjectOutputStream(final HierarchicalStreamWriter writer, final String rootNodeName, final DataHolder dataHolder)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. |
| public void | denyPermission(TypePermission
the permission to deny permission)Add security permission denying another one. |
| public void | denyTypeHierarchy(Class<T>
the base type to forbid type)Add security permission forbidding a type hierarchy. |
| private void | |
| public void | |
| public void | |
| public void | denyTypesByRegExp(String[]
the regular expressions to forbid type names regexps)Add security permission forbidding types matching one of the specified regular expressions. |
| public void | denyTypesByRegExp(Pattern[]
the regular expressions to forbid type names regexps)Add security permission forbidding types matching one of the specified regular expressions. |
| public void | denyTypesByWildcard(String[]
the patterns to forbid names patterns)Add security permission forbidding types matching one of the specified wildcard patterns. |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| public Object | |
| public Object | fromXML(InputStream input, Object root)
Deserialize an object from an XML InputStream, populating the fields of the given root object instead of instantiating a new one. |
| public ClassLoader | |
| public ClassLoaderReference | Returns: the referenceRetrieve the reference to this instance' ClassLoader. |
| public ConverterLookup | |
| public Mapper | |
| public ReflectionProvider | |
| public void | |
| public void | ignoreUnknownElements(String
the name pattern as regular expression pattern)Add pattern for unknown element names to ignore. |
| public void | ignoreUnknownElements(final Pattern
the name pattern as regular expression pattern)Add pattern for unknown element names to ignore. |
| public void | marshal(Object obj, HierarchicalStreamWriter writer)
Serialize and object to a hierarchical data structure (such as XML). |
| public void | marshal(Object obj, HierarchicalStreamWriter writer, DataHolder
Extra data you can use to pass to your converters. Use this as you want. If not present,
XStream shall create one lazily as needed. dataHolder)Serialize and object to a hierarchical data structure (such as XML). |
| public DataHolder | Returns: a newDataHolderCreate a DataHolder that can be used to pass data to the converters. |
| public void | |
| public void | processAnnotations(final Class<T>[]
the types with XStream annotations types)Process the annotations of the given types and configure the XStream. |
| public void | processAnnotations(final Class<T>
the type with XStream annotations type)Process the annotations of the given type and configure the XStream. |
| public void | |
| public void | |
| public void | |
| public void | |
| private void | registerConverterDynamically(String className, int priority, Class<T>[] constructorParamTypes, Object[] constructorParamValues)
|
| public void | registerLocalConverter(Class<T>
the class type the field is defined in definedIn, String the field name fieldName, Converter the converter to use converter)Register a local |
| public void | registerLocalConverter(Class<T>
the class type the field is defined in definedIn, String the field name fieldName, SingleValueConverter the converter to use converter)Register a local |
| public void | |
| public void | setCollectionUpdateLimit(int
limit in seconds or 0 to disable check maxSeconds)Set time limit for adding elements to collections or maps. |
| public void | |
| public void | |
| protected void | |
| protected void | |
| protected void | |
| public static void | setupDefaultSecurity(final XStream xstream)
Deprecated
As of 1.4.18
Setup the security framework of a XStream instance.
|
| protected void | |
| private void | |
| protected void | |
| public String | |
| public void | |
| public void | toXML(Object obj, OutputStream out)
Serialize an object to the given OutputStream as pretty-printed XML. |
| public Object | unmarshal(HierarchicalStreamReader reader)
Deserialize an object from a hierarchical data structure (such as XML). |
| public Object | unmarshal(HierarchicalStreamReader reader, Object root)
Deserialize an object from a hierarchical data structure (such as XML), populating the fields of the given root object instead of instantiating a new one. |
| public Object | unmarshal(HierarchicalStreamReader reader, Object
If present, the passed in object will have its fields populated, as opposed to XStream creating a new
instance. Note, that this is a special use case! With the ReflectionConverter XStream will write
directly into the raw memory area of the existing object. Use with care! root, DataHolder Extra data you can use to pass to your converters. Use this as you want. If not present,
XStream shall create one lazily as needed. dataHolder)Deserialize an object from a hierarchical data structure (such as XML). |
| public void | useAttributeFor(String
the name of the field fieldName, Class<T> the Class of the type to be rendered as XML attribute type)Use an attribute for a field or a specific type. |
| public void | useAttributeFor(Class<T>
the Class containing such field definedIn, String the name of the field fieldName)Use an attribute for a field declared in a specific type. |
| public void | useAttributeFor(Class<T>
the Class of the type to be rendered as XML attribute type)Use an attribute for an arbitrary type. |
| protected boolean | |
| protected MapperWrapper |