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.
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.
The build path must contains the following jar:
Change the x's with your version.
xstream.alias("className", myClass.class);
xstream.aliasField("targetFieldName", myClass.class, "sourceFieldName");
// How to make an attribute of a field
xstream.useAttributeFor(myClass.class, "sourceFieldName");
In case of lazy initialization, you can hide a field.
xstream.omitField(classType, fieldName)
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:
When using a map datatype has, you may want to change the default entry value.
xstream.alias("parameter", Map.Entry.class);
A converter is the best way to control the output when using collection.
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.
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.
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.