1

I have a problem with the deserialization with XStream (from a XML to a java object).

The deserialization works well, except for the repeatable elements (which must turn into a List)...

My XML :

<DATA>
    <ANNUAIRES>
        <REC_ANNUAIRE>
            <NOPERS>1815985</NOPERS>
            <NOM>Dubois A. (Evilard)</NOM>
            <LIGNEADR1>DUBOIS A.</LIGNEADR1>
        </REC_ANNUAIRE>
        <REC_ANNUAIRE>
            <NOPERS>0229984</NOPERS>
            <NOM>Dubois Abel (La Chaux-de-Fonds)</NOM>
            <LIGNEADR1>DUBOIS ABEL</LIGNEADR1>
        </REC_ANNUAIRE>
        <REC_ANNUAIRE>
            <NOPERS>0013717</NOPERS>
            <NOM>Dubois Achim (Corpataux-Magnedens)</NOM>
            <LIGNEADR1>DUBOIS ACHIM</LIGNEADR1>
        </REC_ANNUAIRE>
        <REC_ANNUAIRE>
            <NOPERS>1602681</NOPERS>
            <NOM>Dubois Adrienne (Orvin)</NOM>
            <LIGNEADR1>DUBOIS ADRIENNE</LIGNEADR1>
        </REC_ANNUAIRE>
        <REC_ANNUAIRE>
            <NOPERS>0384177</NOPERS>
            <NOM>Dubois Agnes (Thun 7)</NOM>
            <LIGNEADR1>DUBOIS AGNES</LIGNEADR1>
        </REC_ANNUAIRE>
        <REC_ANNUAIRE>
            <NOPERS>1424276</NOPERS>
            <NOM>Dubois Agnes (Bussigny-Lausanne)</NOM>
            <LIGNEADR1>DUBOIS AGNES</LIGNEADR1>
        </REC_ANNUAIRE>
        <NBREC EOF="False">50</NBREC>
        <DUREE_EXECSQL>0</DUREE_EXECSQL>
        <DUREE_BUILDXML>0</DUREE_BUILDXML>
    </ANNUAIRES>
</DATA>

My XStream Code :

XStream xs = new XStream();

xs.alias("DATA", PojoPersonne.class);
xs.alias("REC_ANNUAIRE", Personne.class);

xs.aliasField("ANNUAIRES", PojoPersonne.class, "annuaire");
xs.aliasField("NOPERS", Personne.class, "idPersonne");
xs.aliasField("NOM", Personne.class, "identite");
xs.aliasField("STATUS", PojoPersonne.class, "status");


xs.addImplicitCollection(PojoPersonne.class, "listePersonne");


PojoPersonne pojo = (PojoPersonne)xs.fromXML(xml);
System.out.println(pojo);
pojo.afficheListe();

My PojoPersonne class :

public class PojoPersonne {
    private String annuaire;
    private List<Personne> listePersonne = new ArrayList<>();
    private String status;

    public PojoPersonne(String annuaire, ArrayList<Personne> listePersonne, String status){
        this.annuaire = annuaire;
        this.listePersonne = listePersonne;
        this.status = status;
    }
    public PojoPersonne(){
        this.listePersonne = new ArrayList<>();
    }
}

So the problem is from the listePersonne ArrayList... it's always null (or empty if I use XStream xs = new XStream(new PureJavaReflectionProvider());)

Thanks for your help

user2472508
  • 99
  • 1
  • 5

1 Answers1

1

You need to create a class for the ArrayList and use:

xstream.addImplicitCollection(Annuaries.class, "ANNUAIRES");

Also lose the <DATA></DATA> you don't need it. Should be something like explained in this link

Community
  • 1
  • 1
TFT
  • 134
  • 4
  • Thanks, but I can't do without ``, if I try to ignore the root element, I have an Exception (`CannotResolveClassException: DATA`). But with your help, it's much better... Nearly. I have another problem now. When I try to recover the result (ListePersonne pojo = (`ListePersonne)xs.fromXML(xml);`), the cast doesn't work (`vaudoiseLib.pojo.personne.ListePersonne cannot be cast to vaudoiseLib.model.personne.Personne`). I think it's maybe because of my XML (` ... `) but I don't know how I can resolve that... – user2472508 Jul 04 '13 at 20:09
  • P.S. I just try too delete the DATA root element "manually" from the XML (so I use ANNUAIRES like root) and everything works well... But I cannot edit the XML, so I have to do with this.. :( I don't have any idea... – user2472508 Jul 04 '13 at 20:32
  • Just do another class, Data.class. This class has what? An annuaries instance. I'll show you a working example when I'm back at the office – TFT Jul 04 '13 at 23:12
  • Thanks ! I understand well how that work ! Everything is ok now =) – user2472508 Jul 05 '13 at 07:13