XStream 如何使用注解定义别名

@XStreamAlias("ContractRoot")
public class ContractRoot {
@XStreamAlias("TcpCont")
private TcpCont TcpCont;
@XStreamAsAttribute//以属性的形式出现
@XStreamAlias(" SvcCont")
private SvcCont SvcCont;
public TcpCont getTcpCont() {
return TcpCont;
}
public void setTcpCont(TcpCont TcpCont) {
this.TcpCont = TcpCont;
}
public SvcCont getSvcCont() {
return SvcCont;
}
public void setSvcCont(SvcCont SvcCont) {
this.SvcCont = SvcCont;
}
}

public static String oject2String(Object object) {
XStream xstream = new XStream();
//设置使用注解方式来处理别名信息
xstream.processAnnotations(object.getClass());
return xstream.toXML(object);
}

@SuppressWarnings("unchecked")
public static T xml2Object(String xml, Class cls) {
XStream xStream = new XStream(new DomDriver());
xStream.processAnnotations(cls);
T obj = (T) xStream.fromXML(xml);
return obj;
}

你可能感兴趣的:(XStream)