当前位置: 移动技术网 > IT编程>开发语言>Java > xml与Java对象的转换详解

xml与Java对象的转换详解

2019年07月22日  | 移动技术网IT编程  | 我要评论
xml与java对象的转换详解 1.xstream解析报文 xstreamcomponent x = xstreamcomponent.newinstanc

xml与java对象的转换详解

1.xstream解析报文

xstreamcomponent x = xstreamcomponent.newinstance();
 x.processannotations(new class[]{equityexchangedetail.class,pearticketcustomerdto.class,date.class,integer.class});
 equityexchangedetail ptd = (equityexchangedetail) x.fromxml(xml);

2.xstream封装报文

xstreamcomponent xres = xstreamcomponent.newinstance();
    xres.processannotations(new class[]{transresult.class});
 string result=xres.toxml(transresult);

3.注解:

@xstreamalias("customerinfo")  //报文中<customerinfo>节点对应类名“pearticketcustomerdto”
public class pearticketcustomerdto {
@xstreamalias("idno")   //报文中<idno>节点对应类属性“idno”
 private string idno;
@xstreamomitfield
private long ticketid;  //报文中无<ticketid>节点 ,解析时忽略类属性ticketid

4.方法比较

x.processannotations(new class[]{pearticketdto.class}):读取类名注解
x.alias(new class[]{pearticketdto.class}):不读取类名注解

5.解析报文

x.alias("equities", list.class);--把报文节点<equities> 转化为list对象
x.alias("equity", equity.class);--把报文节点<equity> 转化为equity类对象
list<equity> equities = (list<equity>) x.fromxml(xml);--开始转化

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网