欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

xml与Java对象的转换详解

程序员文章站 2024-03-31 13:12:46
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);--开始转化

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