XStream解析XMl文件2

1、新建QueryOrders.java文件:

/**
 * 
 */
package com.saicfc.pmpf.resume.bocom.utils.resume;

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

/**
 * @author lizhiyong
 * 
 */
@XStreamAlias("BOCOMB2C")
public class QueryOrders {
    
    public OpRep opRep;
    
    public OpRep getOpRep() {
        return opRep;
    }

    public class OpRep{
        public String retCode;
        
        public String errMsg;
        
        public OpResult opResult;
        
        public OpResultSet opResultSet;

        public String getRetCode() {
            return retCode;
        }

        public String getErrMsg() {
            return errMsg;
        }
        
        public OpResult getOpResult() {
            return opResult;
        }
        
        public OpResultSet getOpResultSet() {
            return opResultSet;
        }
        
    }
    
    public class OpResult{
        public String merchantID;

        public String getMerchantID() {
            return merchantID;
        }
    }
    
    public class OpResultSet{
        @XStreamImplicit(itemFieldName="opResult")
        public List<OpResults> opResults;

        public List<OpResults> getOpResults() {
            return opResults;
        }
    }
    
    public class OpResults{
        public String order;
        
        public String orderDate;
        
        public String orderTime;
        
        public String curType;
        
        public String amount;
        
        public String tranDate;
        
        public String tranTime;
        
        public String tranState;
        
        public String orderState;
        
        public String fee;
        
        public String bankSerialNo;
        
        public String bankBatNo;
        
        public String cardType;
        
        public String merchantBatNo;
        
        public String merchantComment;
        
        public String bankComment;

        public String getOrder() {
            return order;
        }

        public String getOrderDate() {
            return orderDate;
        }

        public String getOrderTime() {
            return orderTime;
        }

        public String getCurType() {
            return curType;
        }

        public String getAmount() {
            return amount;
        }

        public String getTranDate() {
            return tranDate;
        }

        public String getTranTime() {
            return tranTime;
        }

        public String getTranState() {
            return tranState;
        }

        public String getOrderState() {
            return orderState;
        }

        public String getFee() {
            return fee;
        }

        public String getBankSerialNo() {
            return bankSerialNo;
        }

        public String getBankBatNo() {
            return bankBatNo;
        }

        public String getCardType() {
            return cardType;
        }

        public String getMerchantBatNo() {
            return merchantBatNo;
        }

        public String getMerchantComment() {
            return merchantComment;
        }

        public String getBankComment() {
            return bankComment;
        }
    }
    
	public String signData;

    public String getSignData() {
        return signData;
    }
}

2、解析XML文件:

XStream xStream = new XStream(new DomDriver());
        xStream.processAnnotations(QueryOrders.class);
        QueryOrders queryOrders = (QueryOrders) xStream.fromXML(result);
        OpRep opRep = queryOrders.getOpRep();
        String retCode = opRep.getRetCode(); //返回码
        String errMsg = opRep.getErrMsg(); //错误描述

        OpResult opResult = opRep.getOpResult();
        String merchantID = opResult.getMerchantID();//商户号
        OpResultSet opResultSet = opRep.getOpResultSet();

        List<OpResults> list = opResultSet.getOpResults();

        List recList = new ArrayList();
        Map<String, Object> recMap = null;
        if (CommonUtils.isNotEmpty(list)) {
            for (Iterator iterator = list.iterator(); iterator.hasNext();) {
                OpResults opResults = (OpResults) iterator.next();
                recMap = new HashMap<String, Object>();
                recMap.put("order", opResults.getOrder());//订单号
                recMap.put("orderDate", opResults.getOrderDate());//订单日期
                recMap.put("orderTime", opResults.getOrderTime());//订单时间
                recMap.put("curType", opResults.getCurType());//币种
                recMap.put("amount", opResults.getAmount());//金额
                recMap.put("tranDate", opResults.getTranDate());//支付日期
                recMap.put("tranTime", opResults.getTranTime());//支付时间

                recMap.put("tranState", opResults.getTranState());//支付交易状态
                recMap.put("orderState", opResults.getOrderState());//订单状态
                recMap.put("fee", opResults.getFee());//手续费
                recMap.put("bankSerialNo", opResults.getBankSerialNo());//银行流水号
                recMap.put("bankBatNo", opResults.getBankBatNo());//银行批次号
                recMap.put("cardType", opResults.getCardType());//交易卡类型
                recMap.put("merchantBatNo", opResults.getMerchantBatNo());//商户批次号
                recMap.put("merchantComment", opResults.getMerchantComment());//商户备注
                recMap.put("bankComment", opResults.getBankComment());//银行备注
                recList.add(recMap);
            }
        }
        String signData = queryOrders.getSignData();//签名数据
        //需要去验签的源数据
        String msg = "</opRep>";
        String originalStr = result.substring(result.indexOf("<opRep>"),
            result.indexOf(msg) + msg.length());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("retCode", retCode);
        map.put("errMsg", errMsg);
        map.put("merchantID", merchantID);
        map.put("signData", signData);
        map.put("originalStr", originalStr);
        map.put("recList", recList);
        return map;


你可能感兴趣的:(xstream)