这对我有用:
@XStreamAlias("a")
public class A {
}
@XStreamAlias("b")
public class B extends A {
}
@XStreamAlias("c")
public class C {
private ArrayList<A> aList = null;
public C() {
aList = new ArrayList<A>();
aList.add(new A());
aList.add(new B());
aList.add(new A());
}
public static void main(String[] args) {
C c = new C();
XStream x = new XStream();
x.processAnnotations(A.class);
x.processAnnotations(B.class);
x.processAnnotations(C.class);
System.out.println(x.toXML(c));
}
}
并产生:
<c>
<aList>
<a/>
<b/>
<a/>
</aList>
</c>
如果您不想为每个可能的类调用processAnnotations,您可以使用反射API编写一些动态代码,并在循环中处理所有类。