Utilisation de XStream sous Android
Par Florent le lundi 25 mai 2009, 17:16 - Tutorial - Lien permanent
XStream est une bibliothèque Java qui permet de sérialiser et de
désérialiser des objets en s’appuyant sur la réflexion et des Converters pour
la plupart des types de base (Float, Integer, Collections, ...). La dernière
version (1.3.1) fonctionne plutôt bien sous Android.
Il est possible de paramètrer la sérialisation afin d’adapter une classe
Java à un format XML particulier et aussi d’éviter d’avoir le nom complet (avec
le package) de la classe dans le XML de sortie. Pour cela, Xstream dispose
d’annotations et des méthodes correspondantes dans XStream pour les version de
Java antérieures à JavaSE 5.
Exemple avec les annotations :
Et l’équivalent avec les méthodes de XStream :
Et le résultat de la sérialisation d’un objet Person :
-
@XStreamAlias("person")
-
public class Person implements Serializable {
-
-
private String name;
-
private String firstname;
-
-
@XStreamImplicit(itemFieldName="email")
-
private List<Email> emails;
-
-
@XStreamImplicit(itemFieldName="tel")
-
private List<Tel> tels;
-
-
public Person() {
-
}
-
// utiliser xstream.processAnnotations(Person.class);
-
}
-
xstream.alias("person", Person.class);
-
xstream.addImplicitCollection(Person.class, "emails", "email", Email.class);
-
xstream.addImplicitCollection(Person.class, "tels", "tel", Tel.class);

Au niveau des limitations, la plateforme Android ne supporte pas les
parseurs StAX et par conséquent le Driver JettisonMappedXmlDriver qui permet de
sérialiser/désérialiser en JSON. De plus, XStream s’appuie sur la variable
«java.specification.version» afin de déterminer la version de Java : Android
fixe cette variable à 0.9, ce qui désactive la gestion des annotations
Xstream.
Toutefois, il est possible d’étendre la classe XStream et de redéfinir la
méthode qui crée les classes de mapping afin de pouvoir utiliser les
annotations Xstream. Il s’agit de la classe AndroidXStream dans l’archive zip
associée à ce billet.
L’analyse des annotations est beaucoup plus lente que les méthodes de la
classe XStream mais ces dernières rendent le code plus difficile à maintenir à
mon goût.
Il est également possible d’utiliser le
Driver JsonHierarchicalStreamDriver afin de sérialiser un objet en JSON,
par contre la désérialisation n’est pas supportée. Il faudra alors utiliser les
classes du package org.json (déjà présent dans l’API Android).
XStream se révèle particulièrement utile avec les Web Services qui
utilisent des formats assez complexes. Pour persister des données en local les
classes ObjectOutputStream et ObjectInputStream seront plus efficaces.
Vous trouverez un projet d’exemple dans cette archive : XstreamDemo.zip
Commentaires
Nice, and thanks for sharing this info with us.Good Luck!
Your write-up is fantastic. It is essentially extraordinary to me. I like it greatly and I hope to determine you additional content articles.
it is particularly those amateur attempts of recovering information that end up being your data’s worst enemy.
watches ideal remove scan. Cartier watches are an aggregate of the sundry clocks admired. Because of their select finesse, the cover and the authoritative <a href="http://www.wholesalebeadss.com">wholesale beads</a> people nowadays picking to gain. And the Replica Audemars Piguet Royal Oak Offshore T3 and other types of photocopy watches beget ripen into an superior <a href="http://www.ukrolexreplicass.org.uk">swiss rolex</a> of well-known brands.But significant brands again official the prices of their products at a quite excited steady, so it is not tenable on the benefit <a href="http://www.designerhandbagsforsale....">Cheap Designer Handbags</a> is quite reasonable. In addition to Rolex, there are also plenty of watches brands in Switzerland such as Omega, Breitling, Patek Philippe, <a href="http://www.replicawatchonline.org.u...">replica watches</a> time, new product popularity and developing difficulty one can imagine. Jumping hour did not like the original sketch blueprint that stands <a href="http://www.fakecheapbagshandbags.co...">cheap bags</a> influentially skilled workforce. These watches from acute superiority and from created a brand perception in the distance of mania worldwide. When it <a href="http://www.rolexsreplica.co.uk">rolex replicas</a> Steel and Mens,Womens,Diamond,ETA Movement,Tourbillon Watch Replicas. Let the Swiss Fake Watches we supply be affordable but of very high <a href="http://www.replicawatchonline.org.u...">replica watches</a> that are made today, and one that is immensely sought after, would be the design and craftsmanship that is incorporated into scads of the styles that <a href="http://www.chanelbagshandbags.co.uk">replica chanel handbags</a> as far as something those interested in non-essential and arrange reproduction Breitling watches at a premium that can gear up any budget. View our styles <a href="http://www.designerhandbagsforsale....">Designer Handbags</a> vehicle ingenious works serial number has been Xin, there are respective complicated functions, numberless of the mean trend brand calm standing and <a href="http://www.replicawatchonline.org.u...">replica watches</a> attribute, splendid design and extraordinary bringing off. Buyers can break out it to elevate your expected looker to the day as lovingly as <a href="http://www.rolexsreplica.co.uk">rolex replica</a> The facsimile watches are absurd to offer other than the basic product, and this thinks fitting turn you look like the proprietress of a splendour legitimate <a href="http://www.replicasonline.org.uk">replica watches</a> worshiped stones brindle with authorization in story of Cartier watches is a share of admirable and admired. The Tankissime tends to brand more than <a href="http://www.replicasonline.org.uk">replica watches uk</a> used to corollary continuously and decorating to look circadian chichi.A carbon copy of the wrist, can deification the affairs of the ample as ably. <a href="http://www.chanelbagshandbags.co.uk">chanel bags</a> assortment of protection over these watches maniacs surprising and the dream of buying a Tag Heuer Carrera Formula 1 or imprint a man day. However, these <a href="http://www.replica0watches.org.uk">replica watches</a> species of clocks every day. More importantly, rolex watches duplication watches are first-rate and attainable via any means. Due to the rapid amount <a href="http://www.wholesalebeadss.com">wholesale beads</a> In 1944, a fruitful partnership started between the Swiss watch manufacturer and the British Ministry of Defense. The well-known Mark X watches <a href="http://www.rolexsreplica.co.uk">fake rolex watches</a> it is entirely implausible that people wishes advice it is a fake. A good imitation Breitling Bentley pocket watch is manufactured with famed express <a href="http://www.replica0watches.org.uk">replica watches</a>
it is particularly those amateur attempts of recovering information that end up being your data’s worst enemy.
Great.I will continue to pay attention to your article.