About
XStream is not a data binding tool. It is a serialization tool.
The architecture of XStream consists of the four main components:
Component | Description |
---|---|
Converters | The responsibility of a Converter is to provide a strategy for converting particular types of objects found in the object graph, to and from XML. |
Drivers (Writer and Reader) | This abstraction allows XStream to read XML from direct streams using an XML parser or directly manipulate existing structures (such as DOM). XStream comes bundled with reader and writer implementations for most major XML libraries. |
Context | |
Facade | The main XStream class is typically used as the entry point. This assembles the necessary components of XStream |
The XStream class is just a facade - it can always be bypassed for more advanced operations.
Articles Related
Sax
XStream works on a stream-based parser model, while SAX is event-based. The stream based model implies, that the caller consumes the individual tokens from the XML parser on demand, while in an event-based model the parser controls the application flow on its own and will use callbacks to support client processing. The different architecture makes it therefore impossible for XStream to use an event-driven XML parser.
Build Path
The build path must contains the following jar:
- x-stream-x.x.x.jar
- xmlpul-x.x.x.x.jar (for serializing)
- xpp3_min-x.xxx.jar (for Deserializing)
Change the x's with your version.
Snippets
Class
Aliasing
xstream.alias("className", myClass.class);
Field
Aliasing
xstream.aliasField("targetFieldName", myClass.class, "sourceFieldName");
Attribute
// How to make an attribute of a field
xstream.useAttributeFor(myClass.class, "sourceFieldName");
Omit
In case of lazy initialization, you can hide a field.
xstream.omitField(classType, fieldName)
Collections
Implicit
You tag a collection as implicit if you don't need to display it's root tag.
The addImplicitCollection method tag which class and which member collection variable that will be declared as implicit.
xstream.addImplicitCollection(ResultCompound.class, "results");
where:
- ResultCompound.class is the class
- results is a collection field of the above class
Others
Map
When using a map datatype has, you may want to change the default entry value.
xstream.alias("parameter", Map.Entry.class);
Converter
A converter is the best way to control the output when using collection.
Support
java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:336)
at com.gerardnico.Test.main(Test.java:22)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Don't forget to add in your build path the xmlpull.jar.
Cannot create XmlPullParser
Exception in thread "main" com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser
at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:56)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1011)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1002)
at com.gerardnico.Test.main(Test.java:34)
Don't forget to add in your build path the xpp3_min.jar.
CannotResolveClassException
Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: unit
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:79)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
.........
When unmarshalling, the alias of the class is mandatory.