Xstream – Introduction

What is XStream

XStream is a java library to convert Java Object into XML and back. You can take nearly every arbitrary deeply nested object turn it to XML and read it back without further configuration. You’re done if all you want to have is a persisted object in XML. XStream’s power lies in its extensive list of ‘Converters’ that help convert a wide variety of Java Objects to XML and vice versa. A Converter is a class that specifies how a Java Object should be serialized to XML. XStream comes bundled with a lot of Converters and you can also customize your own.

XStream allows converting a class to XML without modifying the class. It can serialize private and final fields and also inner classes. It can serialize references to objects. XStream has also added security measures that allows you to selectively de-serialize and serialize based on type.

These tutorials provide an indepth look at all major XStream functionalities and presents examples as well as explanations that significantly reduce the learning curve that is generally experienced with any open source library.

Converting a Java Object to XML and back – Parsing an XMLIn this tutorial we explain how to build a java object to read an XML. The objective is to create a java object and convert it to an XML which looks similar to the XML that we want to read. We then simply use this java object and XStream to parse our XML. This example introduces the basic XStream usage and also explains field and attribute ‘alias’ and ‘ImplicitCollection’/’ImplicitArray’

XML to Java – We look at some more examples of XML to Java conversion. For example, we see how a piece of XML can be converted to either a List of String or a String array.

Java to XML – In this tutorial we learn how to serialize java to XML. The example demonstrates the use of aliases and implicit collection and also shows how the conversion looks if we dont use alias or implicit collectsion

Java to XML Annotation – This tutorial shows how to serialize Java to XML using annotations.

Handling Attributes in XML – Handling attributes requires special methods since an element may contain an attribute with or without a value. In this tutorial we look at three ways of handling attributes – Element with attributes but no value, element with attributes but no value (using aliases), element with attribute and value (ToAttributedValueConverter)

Referencing Objects – A java class may have fields that store reference to other objects. There may be cases where a circular reference is encountered. XStream has multiple ways to store references to other objects. In this tutorial we look at all modes of storing references using XStream.

Custom Converter for BufferedImage – The power of XStream lies in its ability to convert a java class to XML and vice versa. Its does so using classes called ‘converters’. These converters have methods for marshalling and unmarshalling java and XML. XStream provides a lot of basic converters (basic java classes), Collection Converters and extended converters (converts third party java classes). However, if there is a class for which no converters are available then XStream provides a way to implement a custom converter. In this example we learn how to develop a custom converter for a BufferedImage.

Basic Built-in Converters – This example shows some of the basic converters in XStream.

Collection Converters – This example shows the collection converters in XStream.

Json to Java – XStream also provides a way to convert a Json to java object. It has two drivers to do so. In this tutorial we learn how to convert json to java using XStream.

XML Transformation – XStream provides a TraxSource that can be used as a source for XSLT transformation. The TraxSource uses the Java Object that is to be transformed and an XStream object.

We thank Jörg Schaible and Joe Walnes for their valuable feedback in making this tutorial better.

We hope this tutorials help you in your work and we would appreciate if you could leave us your feedback and help us reach other developers by sharing our site on facebook and google.

Leave a Reply

Your email address will not be published. Required fields are marked *