我不相信你可以用简单的别名来做到这一点。但是您可以使用如下所示的自定义序列化程序来做到这一点。xstream 文档有一个很好的转换器教程,其中包含有关如何注册自定义转换器的详细信息。
public class Nodo {
String id;
ArrayList<Nodo> children = new ArrayList<>();
}
public class NodoConverter implements Converter {
/** {@inheritDoc} */
@Override
public boolean canConvert(Class arg0) {
return arg0.equals(Nodo.class);
}
/** {@inheritDoc} */
@Override
public void marshal(Object arg0, HierarchicalStreamWriter writer, MarshallingContext arg2) {
marshal((Nodo) arg0, writer, arg2, "nodo");
}
private void marshal(Nodo nodo, HierarchicalStreamWriter writer, MarshallingContext ctx, String elname) {
writer.addAttribute("id", nodo.getId());
for (Nodo child : nodo.children) {
writer.startNode("child");
ctx.convertAnother(child);
writer.endNode();
}
}
/** {@inheritDoc} */
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext ctx) {
Nodo nodo = new Nodo();
nodo.setId(reader.getAttribute("id"));
while (reader.hasMoreChildren()) {
reader.moveDown();
Nodo child = (Nodo) ctx.convertAnother(nodo, Nodo.class);
nodo.children.add(child);
reader.moveUp();
}
}
}