xml与Java对象的转换 xml与Java对象的转换详解
人气:0想了解xml与Java对象的转换详解的相关内容吗,在本文为您仔细讲解xml与Java对象的转换的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:xml与Java对象的转换,xml与Java对象转换的实例,下面大家一起来学习吧。
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);--开始转化
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
加载全部内容