使用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>
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
//这个是反序列化是实现的方法 @Overridepublic 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 blocke.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 blocke.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 blocke.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 blocke.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 blocke.printStackTrace();}}reader.moveUp();}return sendXML;}
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);