1

我对 XStream 的反序列化(从 XML 到 java 对象)有疑问。

反序列化效果很好,除了可重复的元素(必须变成一个列表)......

我的 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>

我的 XStream 代码:

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();

我的 PojoPersonne 课程:

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<>();
    }
}

所以问题出在listePersonne ArrayList ...它始终为空(如果我使用,则为空XStream xs = new XStream(new PureJavaReflectionProvider());

谢谢你的帮助

4

1 回答 1

1

您需要为 ArrayList 创建一个类并使用:

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

也失去了<DATA></DATA>你不需要的。应该类似于此链接中的解释

于 2013-07-04T18:37:16.717 回答