如何使用XStream解组特定的子类?

时间:2016-05-11 15:09:03

标签: java xstream

所以,我有两个类,都是另一个类的子类,我的XML可以是,也是由父类命名并由id属性标识。

如何让XStream解组到正确的类?

代码:

@XStreamAlias("Extension")
public class Extension {

    @XStreamAsAttribute
    @XStreamAlias("Id")
    private String id;

    @XStreamAsAttribute
    @XStreamAlias("Required")
    private boolean required;

    /* getters and setters */

}

@XStreamAlias("Extension")
public class ULExtension extends Extension {

    @XStreamAlias("ULData")
    private ULData ULData;
    /* getter and setter */
}

@XStreamAlias("Extension")
public class XTExtension extends Extension {

    @XStreamAlias("ExtConverter")
    private ExtConverter extConverter;
    /* getter and setter */
}

进入的XML看起来像这样

<Extension Id="UL" Required="false"><ULData><stuffAndThings/></ULData></Extension>

<Extension Id="XT" Required="false"><ExtConverter><otherStuffAndThings/></ExtConverter></Extension>

0 个答案:

没有答案