XStream 对xml的转换

前端之家收集整理的这篇文章主要介绍了XStream 对xml的转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
xpp3_min-1.1.4c.jar
xstream-1.4.1.jar
xmlpull-1.1.3.1.jar

需要三个jar文件
package com.safein.module.xml.test;

import java.util.LinkedList;

/**
* 主类
* @author julong
* @date 2016-10-11 下午03:43:54
*
*/
public class SealXml {

private String resultCode;//响应返回状态码
private String resultMsg;//响应返回错误信息
private String validateCode;//是否验证票据的标识
private String securityCode;// 防伪码
private String imageStr;//base64图片
private String dealCode;//交易码
private String serialNumber;//交易流水
private String dealTime;//交易时间
private String cupboardUserCode;//交易柜员号

private LinkedList<VoucherXml> vouchers = new LinkedList<VoucherXml>();

public String getResultCode() {
return resultCode;
}

public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}

public String getResultMsg() {
return resultMsg;
}

public void setResultMsg(String resultMsg) {
this.resultMsg = resultMsg;
}

public String getValidateCode() {
return validateCode;
}

public void setValidateCode(String validateCode) {
this.validateCode = validateCode;
}

public String getImageStr() {
return imageStr;
}

public void setImageStr(String imageStr) {
this.imageStr = imageStr;
}

public String getDealCode() {
return dealCode;
}

public void setDealCode(String dealCode) {
this.dealCode = dealCode;
}

public String getSerialNumber() {
return serialNumber;
}

public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}

public String getDealTime() {
return dealTime;
}

public void setDealTime(String dealTime) {
this.dealTime = dealTime;
}

public String getCupboardUserCode() {
return cupboardUserCode;
}

public void setCupboardUserCode(String cupboardUserCode) {
this.cupboardUserCode = cupboardUserCode;
}

public String getSecurityCode() {
return securityCode;
}

public void setSecurityCode(String securityCode) {
this.securityCode = securityCode;
}

public LinkedList<VoucherXml> getVouchers() {
return vouchers;
}

public void setVouchers(LinkedList<VoucherXml> vouchers) {
this.vouchers = vouchers;
}

public SealXml() {
super();
}


}



VoucherXml 类

package com.safein.module.xml.test;

/**
* 凭证
* @author julong
* @date 2016-10-11 下午05:02:09
*/
public class VoucherXml {

/**
* 凭证内容
* @author julong
* @date 2016-10-11 下午05:03:27
*/
private String context;

public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}

public VoucherXml() {
super();
}

/**
*
* @param context 凭证内容
* @author julong
* @date 2016-10-11 下午05:04:00
*/
public VoucherXml(String context) {
super();
this.context = context;
}


}

处理方法如下 main

//创建凭据对象信息 VoucherXml voucherInfo1 = new VoucherXml(); voucherInfo1.setContext("1233333333333333333333333333"); VoucherXml voucherInfo2 = new VoucherXml(); voucherInfo2.setContext("1233333333333333333333333333"); VoucherXml voucherInfo3 = new VoucherXml(); voucherInfo3.setContext("1233333333333333333333333333"); VoucherXml voucherInfo4 = new VoucherXml(); voucherInfo4.setContext("1233333333333333333333333333"); LinkedList<VoucherXml> voucherInfos = new LinkedList<VoucherXml>(); voucherInfos.addLast(voucherInfo1); voucherInfos.addLast(voucherInfo2); voucherInfos.addLast(voucherInfo3); voucherInfos.addLast(voucherInfo4); SealXml sealInfo = new SealXml(); sealInfo.setResultCode("000000"); sealInfo.setResultMsg(""); sealInfo.setVouchers(voucherInfos); //对象组装完毕 //开始创建对象 XStream xStream = new XStream(); xStream.processAnnotations(SealXml.class); //修改xml标签名称 xStream.alias("sealXml",SealXml.class); xStream.alias("voucher",VoucherXml.class); String str = xStream.toXML(sealInfo); System.out.println("==================组装报文结果===================="); System.out.println(str); System.out.println("================开始解析================"); SealXml sealInfo1 = (SealXml) xStream.fromXML(str); System.out.println("解析请求和返回报文结果:"+new ObjectMapper().writeValueAsString(sealInfo1)); String str1 = "<sealXml><resultCode>000000</resultCode><resultMsg></resultMsg><vouchers><voucher><context>1233333333333333333333333333</context> </voucher> <voucher> <context>1233333333333333333333333333</context> </voucher> <voucher> <context>1233333333333333333333333333</context> </voucher> <voucher> <context>1233333333333333333333333333</context> </voucher> </vouchers></sealXml>"; XStream xStream1 = new XStream(); xStream1.processAnnotations(SealXml.class); xStream1.alias("sealXml",SealXml.class); xStream1.alias("voucher",VoucherXml.class); System.out.println("================开始解析================"); SealXml sealInfo2 = (SealXml) xStream1.fromXML(str1.toString());

原文链接:https://www.f2er.com/xml/294711.html

猜你在找的XML相关文章