Java类和xml文件的相互转换
前两天公司做一个工行支付的项目,数据传递都是通过xml形式加密传递的,在网上看了相关的资料,感觉都不算很好,有的理论讲的好,可是没有参考代码。有的有代码,但是代码不全,今天抽空整理出一份真实的项目案例,和完整代码。供大家参考互相学习
不说废话,直接上代码,相信大家都能看懂,东西很简单,就是使用经验,用一次大家都会了。
这里注重经验,每个注解的意思,根据实战查资料了解。谢谢观赏。
- 列举一下xml效果
----对象解析成xml文件---
<?xml version="1.0" encoding="GBK" standalone="no"?>
<B2CReq>
<interfaceName> </interfaceName>
<interfaceVersion> </interfaceVersion>
<orderInfo>
<orderDate> </orderDate>
<orderid> </orderid>
<amount> </amount>
<installmentTimes> </installmentTimes>
<curType> </curType>
<merID> </merID>
<merAcct> </merAcct>
</orderInfo>
<custom>
<verifyJoinFlag> </verifyJoinFlag>
<Language> </Language>
</custom>
<message>
<goodsID> </goodsID>
<goodsName> </goodsName>
<goodsNum> </goodsNum>
<carriageAmt> </carriageAmt>
<merHint> </merHint>
<remark1> </remark1>
<remark2> </remark2>
<merURL> </merURL>
<merVAR> </merVAR>
<notifyType> </notifyType>
<resultType> </resultType>
<backup1> </backup1>
<backup2> </backup2>
<backup3> </backup3>
<backup4> </backup4>
</message>
</B2CReq>
- java代码
//先把准备生成xml的类,交给这个解析器
//marshall用于解析生成xml
//unmarshall用于xml生成java类
//validate没有仔细研究,感兴趣,可以自己使用
JAXBContext context = JAXBContext.newInstance(B2CReq.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化输出
marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");// 编码格式,默认为utf-8
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xml头信息
System.out.println("----对象解析成xml文件---");
//对象解析成xml文件 这里我为了是xml成对出现,我在里面对象里面的字段都是使用 空格字符串
B2CReq b2CReq = new B2CReq();
OrderInfo orderInfo = new OrderInfo();
b2CReq.setOrderInfo(orderInfo);
Custom custom = new Custom();
b2CReq.setCustom(custom);
Message message = new Message();
b2CReq.setMessage(message);
// marshaller.marshal(b2CReq, System.out);
//把对象解析成string
StringWriter sw = new StringWriter();
marshaller.marshal(b2CReq, sw);
System.out.println(sw.toString().replace("standalone=\"yes\"","standalone=\"no\""));
String s = sw.toString();
//xml转换java类
Unmarshaller unmarshaller = context.createUnmarshaller();
B2CReq b2CReq1 = new B2CReq();
b2CReq1 = (B2CReq) unmarshaller.unmarshal(new StringReader(s));
System.out.println(b2CReq1.getInterfaceVersion());
实体类中的标签以代码方式展现
B2CReq
/**
* Created by liuxin on 16/12/13.
* 请求数据模型
* 目的是映射xml文件
*/
@XmlRootElement(name = "B2CReq")//根的名字,因为默认是首字符小写的,我需要大写,所以我限制了
@XmlAccessorType(XmlAccessType.FIELD)
public class B2CReq implements Serializable {
//接口名称固定的
public String interfaceName=" ";
//接口版本号
public String interfaceVersion=" ";
public OrderInfo orderInfo;
public Custom custom;//看我这个实体的注解,只用使用一个
public Message message;
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public Custom getCustom() {
return custom;
}
public void setCustom(Custom custom) {
this.custom = custom;
}
public String getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
public String getInterfaceVersion() {
return interfaceVersion;
}
public void setInterfaceVersion(String interfaceVersion) {
this.interfaceVersion = interfaceVersion;
}
public OrderInfo getOrderInfo() {
return orderInfo;
}
public void setOrderInfo(OrderInfo orderInfo) {
this.orderInfo = orderInfo;
}
}
Custom
@XmlAccessorType(XmlAccessType.FIELD)
public class Custom {
//联名校验标志
public String verifyJoinFlag=" ";
//语言版本默认中文zh_CN
public String Language=" ";
public String getVerifyJoinFlag() {
return verifyJoinFlag;
}
public void setVerifyJoinFlag(String verifyJoinFlag) {
this.verifyJoinFlag = verifyJoinFlag;
}
public String getLanguage() {
return Language;
}
public void setLanguage(String language) {
Language = language;
}
}
Message
@XmlAccessorType(XmlAccessType.FIELD)
public class Message {
//商品编号
public String goodsID=" ";
//商品名称
public String goodsName=" ";
//商品数量
public String goodsNum=" ";
//已含运费金额
public String carriageAmt=" ";
//商户提示
public String merHint=" ";
//备注字段1
public String remark1=" ";
//备注字段2
public String remark2=" ";
//通知商户URL
public String merURL=" ";
//商户变量
public String merVAR=" ";
//通知类型HS 以http协议post提交
public String notifyType=" ";
//结果发送类型0无论成功失败都发送信息
public String resultType=" ";
//备用字段1
public String backup1=" ";
//备用字段2
public String backup2=" ";
//备用字段3
public String backup3=" ";
//备用字段4
public String backup4=" ";
public String getGoodsID() {
return goodsID;
}
public void setGoodsID(String goodsID) {
this.goodsID = goodsID;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(String goodsNum) {
this.goodsNum = goodsNum;
}
public String getCarriageAmt() {
return carriageAmt;
}
public void setCarriageAmt(String carriageAmt) {
this.carriageAmt = carriageAmt;
}
public String getMerHint() {
return merHint;
}
public void setMerHint(String merHint) {
this.merHint = merHint;
}
public String getRemark1() {
return remark1;
}
public void setRemark1(String remark1) {
this.remark1 = remark1;
}
public String getRemark2() {
return remark2;
}
public void setRemark2(String remark2) {
this.remark2 = remark2;
}
public String getMerURL() {
return merURL;
}
public void setMerURL(String merURL) {
this.merURL = merURL;
}
public String getMerVAR() {
return merVAR;
}
public void setMerVAR(String merVAR) {
this.merVAR = merVAR;
}
public String getNotifyType() {
return notifyType;
}
public void setNotifyType(String notifyType) {
this.notifyType = notifyType;
}
public String getResultType() {
return resultType;
}
public void setResultType(String resultType) {
this.resultType = resultType;
}
public String getBackup1() {
return backup1;
}
public void setBackup1(String backup1) {
this.backup1 = backup1;
}
public String getBackup2() {
return backup2;
}
public void setBackup2(String backup2) {
this.backup2 = backup2;
}
public String getBackup3() {
return backup3;
}
public void setBackup3(String backup3) {
this.backup3 = backup3;
}
public String getBackup4() {
return backup4;
}
public void setBackup4(String backup4) {
this.backup4 = backup4;
}
}
OrderInfo
@XmlAccessorType(XmlAccessType.FIELD)
public class OrderInfo {
//交易时间
public String orderDate=" ";
//订单号
public String orderid=" ";
//订单金额
public String amount=" ";
//分期付款期数
public String installmentTimes=" ";
//支付币种
public String curType=" ";
//商户代码
public String merID=" ";
//商户账号
public String merAcct=" ";
public String getOrderDate() {
return orderDate;
}
public void setOrderDate(String orderDate) {
this.orderDate = orderDate;
}
public String getOrderid() {
return orderid;
}
public void setOrderid(String orderid) {
this.orderid = orderid;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getInstallmentTimes() {
return installmentTimes;
}
public void setInstallmentTimes(String installmentTimes) {
this.installmentTimes = installmentTimes;
}
public String getCurType() {
return curType;
}
public void setCurType(String curType) {
this.curType = curType;
}
public String getMerId() {
return merID;
}
public void setMerId(String merId) {
this.merID = merId;
}
public String getMerAcct() {
return merAcct;
}
public void setMerAcct(String merAcct) {
this.merAcct = merAcct;
}
}