I. Introduction
XStream is a Java API which allows serializing and désérialiser objects in XML files.
The benefits of XStream are mainly:
The ease of use of the API.
No change code objects that you want to serialize.
The quick turnaround and low memory usage.
All sources of this article are available here in the form of a project for Eclipse.
II. Where to find XStream?
XStream is available at the following address http://xstream.codehaus.org.
XStream is available in the form of a library JAR on the menu "Download" of the site.
To read the full article, we will use the latest stable xstream-xxjar obtained on the website (Download).
The library xstream-xxjar is to put into the classpath so examples work.
III. Creating classes to serialize
We will first create two classes, and Article Entete.
The class Article declare a class attribute type Entete.
These two classes contain manufacturers, getters and setters useful.
The class Entete:
Creation of Class Entete
package beans;
import java.util.Date;
public class (Entete
Private String title;
Private dateCreation date;
GetDateCreation public Date () (
DateCreation return;
)
Public void setDateCreation (date dateCreation) (
This.dateCreation = dateCreation;
)
Public String getTitre () (
Return title;
)
Public void setTitre (String title) (
This.titre = title;
)
Public Entete (String title, date dateCreation) (
Super ();
This.titre = title;
This.dateCreation = dateCreation;
)
)
And Article class that uses the class Entete:
Creation of Class Rule
package beans;
public class Article (
Private Entete entete;
Private String synopsis;
Public String getSynopsis () (
Return synopsis;
)
Public Article (Entete entete, String synopsis) (
Super ();
This.entete = entete;
This.synopsis = synopsis;
)
Public void setSynopsis (String synopsis) (
This.synopsis = synopsis;
)
Entete public getEntete () (
Entete return;
)
Public void setEntete (Entete header) (
This.entete = entete;
)
)
IV. Serialization classes
Below is a Serialisation class with a main method containing the code to convert the preceding classes chain XML:
A hand to test the serialization
package tests;
import java.util.Date;
import com.thoughtworks.xstream.XStream;
com.thoughtworks.xstream.io.xml.DomDriver import;
public class (Serialisation
Public static void main (String [] args) (
/ / Instanciation of Class XStream
XStream xstream = new XStream (new DomDriver ());
/ / Instanciation of Class Entete
Entete entete = new Entete ( "The article's title," new Date ());
/ / Instanciation of Class Rule
Article item = new Article (entete, "A synopsis well placed! <strong> With an HTML tag </ strong>");
/ / Conversion of the content of the object article in XML
String xml = xstream.toXML (article);
/ / Display the conversion XML
System.out.println (XML);
)
)
The console display:
The content of the object article converts XML
<beans.Article>
<entete>
<title> Article Title </ title>
<dateCreation> 2006-10-01 18:00:31.187 CEST </ dateCreation>
</ entete>
A synopsis <synopsis> well placed! <strong> with an HTML tag </ strong> </ synopsis>
</ beans.Article>
XStream converts the content of the object article in a nice XML file, it should be noted that special characters are also covered:
-- 'A'
-- <strong> <strong>
Now, to serialize the object article in a file article.xml, the code of the class would be:
Serialization of the object article in a file article.xml
package tests;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
beans.Article import;
beans.Entete import;
import com.thoughtworks.xstream.XStream;
com.thoughtworks.xstream.io.xml.DomDriver import;
public class (Serialisation
public static void main (String [] args) (
Try (
/ / Instanciation of Class XStream
XStream xstream = new XStream (new DomDriver ());
/ / Instanciation of Class Entete
Entete entete = new Entete ( "The article's title," new Date ());
/ / Instanciation of Class Rule
Article item = new Article (entete, "A synopsis well placed! <strong> With an HTML tag </ strong>");
/ / Instanciation a file c: / temp / article.xml
File file = new File ( "c: / temp / article.xml");
/ / Instanciation an output stream file to
/ / C: / temp / article.xml
FileOutputStream fos = new FileOutputStream (file);
Try (
/ / Serialization of the object article in c: / temp / article.xml
Xstream.toXML (article, fos);
) Finally (
/ / This ensures close the flow of whatever happens
Fos.close ();
)
) Catch (FileNotFoundException e) (
E.printStackTrace ();
) Catch (IOException ioe) (
Ioe.printStackTrace ();
)
)
)
V. Deserialisation classes
Now to reload (désérialiser) an object type with Article data serialized in the previous chapter, the code will be:
Désérialisation file article.xml to an object type Article
package tests;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
beans.Article import;
import com.thoughtworks.xstream.XStream;
com.thoughtworks.xstream.io.xml.DomDriver import;
public class (Deserialisation
public static void main (String [] args) (
try (
/ / Instanciation of Class XStream
XStream xstream = new XStream (new DomDriver ());
/ / Redirection file c: / temp / article.xml into a stream
/ / Entry file
FileInputStream fis = new FileInputStream (new File ( "c: / temp / article.xml"));
try (
/ / Désérialisation file c: / temp / article.xml to a new
/ / Object article
Article nouvelArticle = (Article) xstream.fromXML (fis);
/ / Display on the console of the content of the attribute synopsis
System.out.println (nouvelArticle.getSynopsis ());
) Finally (
/ / This ensures close the flow of whatever happens
fis.close ();
)
) Catch (FileNotFoundException e) (
e.printStackTrace ();
) Catch (IOException ioe) (
ioe.printStackTrace ();
)
)
)
VI. A little more ...
In this chapter, some information and examples to go further with API XStream.
VI-A. How to prevent the serialization of an attribute?
By declaring an attribute as a transient, it will not be serialized.
In the following example attribute numerocb will not be serialized:
Preventing the serialization of an attribute
package beans;
public class (Author
private String name;
private String firstName;
Private transient String numerocb;
//... Manufacturers, setters and getters
)
VI-B. The serialization of collections
The serialization of collections with XStream is done very easily, take for example a class store containing a list of objects:
An attribute type List to serialize
package beans;
import java.util.ArrayList;
import java.util.List;
public class (Store
private String name;
/ / Delete "<Object>" if you use a jdk <1.5
List <Object> objects <Object> = new ArrayList ();
//... Manufacturers, setters and getters
)
And the following code, which peuplera class shop with objects and type Article Author:
Serialization collectors
package tests;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
beans.Article import;
beans.Auteur import;
beans.Entete import;
beans.Magasin import;
import com.thoughtworks.xstream.XStream;
com.thoughtworks.xstream.io.xml.DomDriver import;
public class (CollectionSerialisation
public static void main (String [] args) (
/ / Instanciation of Class XStream
XStream xstream = new XStream (new DomDriver ());
/ / Instanciation of Class Entete
Entete entete = new Entete ( "The article's title," new Date ());
/ / Instanciation of Class Rule
Article item = new Article (entete, "A synopsis well placed! <strong> With an HTML tag </ strong>");
/ / Instanciation of Class Author
Author author = new Author ( "LA LA LA LA LA LA," "Starsky & Hutch");
Auteur.setNumerocb ( "1788 1803 0485 1875");
/ / Instanciation of Class Store
Store store = new store ( "Yatoutici");
List items = new ArrayList ();
Objets.add (article);
Objets.add (author);
Magasin.setObjets (objects);
/ / Conversion of the content of the object article in XML
String xml = xstream.toXML (store);
/ / Display the conversion XML
System.out.println (XML);
)
)
The console display:
<beans.Magasin>
<name> Yatoutici </ name>
<objets>
<beans.Article>
<entete>
<title> Article Title </ title>
<dateCreation> 2006-10-01 17:48:48.78 CEST </ dateCreation>
</ entete>
A synopsis <synopsis> well placed! <strong> with an HTML tag </ strong> </ synopsis>
</ beans.Article>
<beans.Auteur>
<name> LA LA LA LA LA LA </ name>
<firstname> Starsky & Hutch </ first name>
</ beans.Auteur>
</ objects>
</ beans.Magasin>
VI-C. How to create aliases for package?
You must have noticed qu'XStream used the names of packages to appoint XML tags, it was not legible.
Fortunately, XStream proposes a system to create aliases on the classes used in the serialization.
Thus by taking our first example of serialization will create aliases as follows:
Creating alias
...
/ / Conversion of the content of the object article in XML
Xstream.alias ( "article", Article.class);
Xstream.alias ( "under", Entete.class);
String xml = xstream.toXML (article);
/ / Display the conversion XML
System.out.println (XML);
...
The console will display tags substituted with alias:
The tag <beans.Article> converted into <article> with aliases
<article>
<entete>
<title> Article Title </ title>
<dateCreation> 2006-10-01 18:08:06.234 CEST </ dateCreation>
</ entete>
A synopsis <synopsis> well placed! <strong> with an HTML tag </ strong> </ synopsis>
</ item>
VII. XStream: a tool for persistence?
To this question, I answer: yes and no! A reply from Normandy you tell me, I explain:
So yes, because XStream can actually save and then recover objects serialized, but only using XML serialization (A note in this connection qu'XStream offers classes very useful as XmlArrayList, XmlSet or XmlMap).
And no, because I associate persistent data with XStream DBMS and it does not know how. To do this we must use tools like Hibernate, Castor, JPox, iBatis, OJB, and so on. and some of these tools know how to use the two forms of storage (or XML DBMS) for persistence.
But if you opt for an XML persistence with XStream then remember:
That no description is necessary to serialize most objects (no "mapping")
XStream uses introspection to find the properties of a bean, they may be declared private, not having getters and setters or XStream regain his grandchildren!
VIII. Conclusion
The use of XStream, we notice very quickly that this API will make many services.
XStream is a library ideal for beginners DOM and SAX other, they will find more facilities in the creation and manipulation of XML files using XStream.
And the purposes of XStream are very numerous, transporting data, use of configuration files, and so on.
For example of use, I called XStream to carry a configuration set in a database to an embedded application to run offline.