這篇文章主要介紹xml與Java之間的轉(zhuǎn)換示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司主營(yíng)皇姑網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),皇姑h5小程序設(shè)計(jì)搭建,皇姑網(wǎng)站營(yíng)銷推廣歡迎皇姑等地區(qū)企業(yè)咨詢
xml與Java對(duì)象的轉(zhuǎn)換詳解
1.xstream解析報(bào)文
XStreamComponent x = XStreamComponent.newInstance(); x.processAnnotations(new Class[]{EquityExchangeDetail.class,PearTicketCustomerDTO.class,Date.class,Integer.class}); EquityExchangeDetail ptd = (EquityExchangeDetail) x.fromXML(xml);
2.xstream封裝報(bào)文
XStreamComponent xres = XStreamComponent.newInstance(); xres.processAnnotations(new Class[]{TransResult.class}); String result=xres.toXML(transResult);
3.注解:
@XStreamAlias("customerInfo") //報(bào)文中節(jié)點(diǎn)對(duì)應(yīng)類名“PearTicketCustomerDTO” public class PearTicketCustomerDTO { @XStreamAlias("idno") //報(bào)文中 節(jié)點(diǎn)對(duì)應(yīng)類屬性“idno” private String idno; @XStreamOmitField private Long ticketId; //報(bào)文中無(wú) 節(jié)點(diǎn) ,解析時(shí)忽略類屬性ticketId
4.方法比較
x.processAnnotations(new Class[]{PearTicketDTO.class}):讀取類名注解 x.alias(new Class[]{PearTicketDTO.class}):不讀取類名注解
5.解析報(bào)文
x.alias("Equities", List.class);--把報(bào)文節(jié)點(diǎn)轉(zhuǎn)化為L(zhǎng)ist對(duì)象 x.alias("Equity", Equity.class);--把報(bào)文節(jié)點(diǎn) 轉(zhuǎn)化為Equity類對(duì)象 List equities = (List ) x.fromXML(xml);--開始轉(zhuǎn)化
以上是“xml與Java之間的轉(zhuǎn)換示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!