使用xStream对xml进行反序列化转为JavaBean

在前面已经介绍使用xStream包处理null值的问题,现在我们一起讨论一下对于使用xStream进行反序列化的问题。
这里我们使用的xml模板为:
<ShippingOrder xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject">
<totalCharge>679.08</totalCharge>
<totalCount>567</totalCount>
<totalWeight>851.21</totalWeight>
<agencyPrice>agencyPrice2O305</agencyPrice>
<billId>billId408KI</billId>
<billingPersonName>billingPersonName2O305</billingPersonName>
<branchName>branchName408KI</branchName>
<chargeType>chargeType408KI</chargeType>
<acceptTime>2011-03-07 09:18:43.0</acceptTime>
<checkTime>2011-03-07 09:18:43.0</checkTime>
<consigneeCardNO>consigneeCardNO2O305</consigneeCardNO>
<consigneeInfomation>
<addressTel>addressTel2O305</addressTel>
<consigneeCode>consigneeCode2O305</consigneeCode>
<consigneeUserID>consigneeUserID2O305</consigneeUserID>
<consignmentStatus>consignmentStatus2O305</consignmentStatus>
<gatheringAmount>gatheringAmount2O305</gatheringAmount>
<gatheringType>gatheringType2O305</gatheringType>
<goodsReceiptPlace>goodsReceiptPlace2O305</goodsReceiptPlace>
<isReturn>false</isReturn>
<linkManName>linkManName2O305</linkManName>
<originalEnterprise>originalEnterprise2O305</originalEnterprise>
<originalNumber>originalNumber2O305</originalNumber>
<remark>remark2O305</remark>
<requestedUnloadedDateTime>2011-03-07 09:18:43.0</requestedUnloadedDateTime>
<telephoneNumber>telephoneNumber2O305</telephoneNumber>
</consigneeInfomation>
<consigneeName>consigneeName2O305</consigneeName>
<consigneeSignName>consigneeSignName2O305</consigneeSignName>
<consignorInfomation>
<addressTel>addressTel2O305</addressTel>
<consignorCode>consignorCode2O305</consignorCode>
<linkManName>linkManName2O305</linkManName>
<loadingPlace>loadingPlace2O305</loadingPlace>
<telephoneNumber>telephoneNumber2O305</telephoneNumber>
</consignorInfomation>
<consignorName>consignorName2O305</consignorName>
<endStationName>endStationName408KI</endStationName>
<goodsInfomations>
<GoodsInfomation>
<chargeType>中文测试</chargeType>
<excursionFreight>344.7</excursionFreight>
<freight>911.83</freight>
<goodsBar>goodsBar2O305</goodsBar>
<goodsBatchNumber>goodsBatchNumber2O305</goodsBatchNumber>
<goodsGuid>fd604f4f-163b-4290-93c3-a2256d4f8fe8</goodsGuid>
<goodsName>goodsName2O305</goodsName>
<goodsSpecification>goodsSpecification2O305</goodsSpecification>
<goodsType>goodsType2O305</goodsType>
<goodsTypeCode>goodsTypeCode2O305</goodsTypeCode>
<grossWeightUnitCode>647.59</grossWeightUnitCode>
<grossWeightValue>882.45</grossWeightValue>
<insurancePrice>insurancePrice2O305</insurancePrice>
<isInsurance>isInsurance2O305</isInsurance>
<packageType>packageType2O305</packageType>
<packageTypeCode>packageTypeCode2O305</packageTypeCode>
<packagesNumber>576</packagesNumber>
<remart>remart2O305</remart>
<volumeUnitCode>volumeUnitCode2O305</volumeUnitCode>
<volumeValue>383.46</volumeValue>
</GoodsInfomation>
<GoodsInfomation>
<chargeType>chargeType2O305</chargeType>
<excursionFreight>769.38</excursionFreight>
<freight>928.05</freight>
<goodsBar>goodsBar2O305</goodsBar>
<goodsBatchNumber>goodsBatchNumber2O305</goodsBatchNumber>
<goodsGuid>b22a30c3-6a8a-435a-a2f9-3c92ef3ccdf9</goodsGuid>
<goodsName>goodsName2O305</goodsName>
<goodsSpecification>goodsSpecification2O305</goodsSpecification>
<goodsType>goodsType2O305</goodsType>
<goodsTypeCode>goodsTypeCode2O305</goodsTypeCode>
<grossWeightUnitCode>168.72</grossWeightUnitCode>
<grossWeightValue>996.43</grossWeightValue>
<insurancePrice>insurancePrice2O305</insurancePrice>
<isInsurance>isInsurance2O305</isInsurance>
<packageType>packageType2O305</packageType>
<packageTypeCode>packageTypeCode2O305</packageTypeCode>
<packagesNumber>738</packagesNumber>
<remart>remart2O305</remart>
<volumeUnitCode>volumeUnitCode2O305</volumeUnitCode>
<volumeValue>119.99</volumeValue>
</GoodsInfomation>
</goodsInfomations>
<isInsurance>false</isInsurance>
<isRouteRecord>1</isRouteRecord>
<labelId>labelId408KI</labelId>
<limitTime>2011-03-07 09:18:43.0</limitTime>
<packageBillId>packageBillId2O305</packageBillId>
<prepayPrice>prepayPrice2O305</prepayPrice>
<procedureCharge>procedureCharge2O305</procedureCharge>
<remark>remark2O305</remark>
<route>route2O305</route>
<routePersonName>routePersonName2O305</routePersonName>
<sendStationName>sendStationName408KI</sendStationName>
<stationName>stationName408KI</stationName>

<transferName>transferName408KI</transferName>
<travelTime>2011-03-07 09:18:43.0</travelTime>
</ShippingOrder>

我们需要四个类,分别为SendXML、Consignee、Consignor和Goods,这里我给出SendXML的源码如下:
public class SendXML {

private String billId;// 托运单号

private String labelId;// 票签号

private String isRouteRecord;//记录类型,0为线路员记录单,1为托运单

private String branchName;// 营业网点名称

private String stationName;// 受理站

private String sendStationName;// 发送站点

private String transferName;// 中转站编号

private String endStationName;// 到达站点

private String chargeType;// 收费模式

private String procedureCharge;// 手续费

private String packageBillId;// 打包托运单号

private String agencyPrice;// 代收货款

private String prepayPrice;// 代垫货款

private String totalCharge;// 总金额

private String totalCount;// 总数量

private String totalWeight;// 总重量

private String isInsurance;//是否保价

private Consignee consignee;// 收获方信息

private Consignor consignor;// 装货方信息

private String consigneeCardNO;// 收获人身份证

private String consigneeSignName;// 收获人签名

private Date travelTime;// 运输时间

private Date acceptTime;// 受理时间

private Date checkTime;// 签发时间

private Date limitTime;// 到货时限

private String billingPersonName;//开单人姓名

private String routePersonName;//线路员

private String remark;//备注

private String route;//终点站(使用)

private String consigneeName;//收货人姓名

private String consignorName;//发货人姓名

private List<Goods> goodsInfomations;//货物信息

//setter、getter

之前看了很多其他的介绍没有使用转换器的,但这里我必须需要写转换器实现,否则无法得到结果。
在这里我写了一个类实现Converter接口,如:

//这个是反序列化是实现的方法
@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {

SendXML sendXML = new SendXML();

while(reader.hasMoreChildren()){
reader.moveDown();
String node = reader.getNodeName();
if (node.indexOf("Time")!=-1) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
Date date = null;
try {
date = sdf.parse(reader.getValue());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String temp = node.substring(0, 1).toUpperCase()+node.substring(1);
Class[] classes = new Class[1];
classes[0] = Date.class;
try {
Method m = SendXML.class.getMethod("set"+temp, classes);
m.invoke(sendXML, date);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if ("consigneeInfomation".equals(node)) {
Consignee cgee = new Consignee();
while(reader.hasMoreChildren()){
reader.moveDown();
String node2 = reader.getNodeName();
if ("requestedUnloadedDateTime".equals(reader.getNodeName())) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
Date date = null;
try {
date = sdf.parse(reader.getValue());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cgee.setRequestedUnloadedDateTime(date);
}else {
String temp = node2.substring(0, 1).toUpperCase()+node2.substring(1);
try {
Class[] classes = new Class[1];
classes[0] = String.class;
Method method = Consignee.class.getMethod("set"+temp, classes);
method.invoke(cgee, reader.getValue());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
reader.moveUp();
}

sendXML.setConsignee(cgee);
}else if("consignorInfomation".equals(node)){
Consignor cgor = (Consignor)context.convertAnother(sendXML, Consignor.class);
sendXML.setConsignor(cgor);
}else if ("goodsInfomations".equals(node)) {
List<Goods> list = new ArrayList<Goods>();
while(reader.hasMoreChildren()){
reader.moveDown();
Goods goods = (Goods)context.convertAnother(sendXML, Goods.class);
list.add(goods);
reader.moveUp();
}
sendXML.setGoodsInfomations(list);
}else {
String temp = node.substring(0, 1).toUpperCase()+node.substring(1);
try {
Class[] classes = new Class[1];
classes[0] = String.class;
Method method = SendXML.class.getMethod("set"+temp, classes);
method.invoke(sendXML, reader.getValue());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
reader.moveUp();
}
return sendXML;
}

在使用到XStream时,需要注册这个转换器,如:
File xmlModel = new java.io.File(filePath);

FileInputStream in = new FileInputStream(xmlModel);

XStream xStream = new XStream(new DomDriver());
xStream.alias("ShippingOrder", SendXML.class);

xStream.registerConverter(new ShippingConverter());
SendXML sendXML = (SendXML)xStream.fromXML(in);

现在基本上就已经满足要求了,但是效率不高。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值