首页 > 代码库 > XStream解析XMl文件

XStream解析XMl文件

1、新建RefundResult.java文件:

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

import java.util.List;

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

/**
 * 解析银行返回数据
 * @author lizhiyong
 * @version $Id: QueryOrders2.java, v 0.1
		2014年7月24日 下午3:30:47 Exp $
 */
@XStreamAlias("TX")
public class RefundResult {
	@XStreamAlias("REQUEST_SN")
	public String requestSn;
	
	@XStreamAlias("CUST_ID")
	public String custId;
	
	@XStreamAlias("TX_CODE")
	public String txcode;
	
	@XStreamAlias("RETURN_CODE")
	public String returnCode;
	
	@XStreamAlias("RETURN_MSG")
	public String returnMsg;
	
	@XStreamAlias("LANGUAGE")
	public String language;
	
	@XStreamAlias("TX_INFO")
	public TxInfo2 txInfo2;
	
	public static class TxInfo2{
	    @XStreamAlias("CUR_PAGE")
	    public String curPage;
	    
	    @XStreamAlias("TPAGE")
        public String tPage;
	    
	    @XStreamImplicit(itemFieldName="LIST")
	    public List<QueryOrder> queryOrders;
	    @XStreamAlias("NOTICE")
	    public String notice;
	    
        public String getCurPage() {
            return curPage;
        }

        public String getNotice() {
            return notice;
        }

        public String gettPage() {
            return tPage;
        }

        public List<QueryOrder> getQueryOrders() {
            return queryOrders;
        }
	    
	} 

	public static class QueryOrder {
		@XStreamAlias("TRAN_DATE")
		public String tranDate;
		
		@XStreamAlias("REFUND_DATE")
		public String refundDate;
		
		@XStreamAlias("ORDER_NUMBER")
		public String orderNumber;

		@XStreamAlias("REFUND_ACCOUNT")
		public String refundAccount;
		
		@XStreamAlias("PAY_AMOUNT")
		public String payAmount;
		
		@XStreamAlias("REFUNDEMENT_AMOUNT")
		public String refundementAmount;
		
		@XStreamAlias("POS_CODE")
		public String posCode;
		
		@XStreamAlias("USERID")
		public String userId;
		
		@XStreamAlias("STATUS")
		public String status;

        public String getTranDate() {
            return tranDate;
        }

        public String getRefundDate() {
            return refundDate;
        }

        public String getOrderNumber() {
            return orderNumber;
        }

        public String getRefundAccount() {
            return refundAccount;
        }

        public String getPayAmount() {
            return payAmount;
        }

        public String getRefundementAmount() {
            return refundementAmount;
        }

        public String getPosCode() {
            return posCode;
        }

        public String getUserId() {
            return userId;
        }

        public String getStatus() {
            return status;
        }
	}

    public String getRequestSn() {
        return requestSn;
    }

    public String getCustId() {
        return custId;
    }

    public String getTxcode() {
        return txcode;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public String getReturnMsg() {
        return returnMsg;
    }

    public String getLanguage() {
        return language;
    }

    public TxInfo2 getTxInfo2() {
        return txInfo2;
    }





}

2、解析XML文件:

XStream xStream = new XStream(new DomDriver());
            xStream.processAnnotations(RefundResult.class);
            RefundResult query = (RefundResult) xStream.fromXML(revResult);
            Map<String, Object> orderMap = new HashMap<String, Object>();
            orderMap.put("returnCode", query.getReturnCode());
            orderMap.put("returnMsg", query.getReturnMsg());
            //交易成功
            if ("000000".equals(orderMap.get("returnCode"))) {
                orderMap.put("requestSn", query.getRequestSn());
                orderMap.put("custId", query.getCustId());
                orderMap.put("txcode", query.getTxcode());
                orderMap.put("language", query.getLanguage());

                TxInfo2 txInfo2 = query.getTxInfo2();
                orderMap.put("curPage", txInfo2.getCurPage());
                orderMap.put("tPage", txInfo2.gettPage());

                List<QueryOrder> queryList = txInfo2.getQueryOrders();

                List queryOrderList = new ArrayList();

                Map<String, Object> queryMap = null;

                for (Iterator iterator = queryList.iterator(); iterator.hasNext();) {
                    QueryOrder queryOrder = (QueryOrder) iterator.next();
                    queryMap = new HashMap<String, Object>();
                    queryMap.put("tranDate", queryOrder.getTranDate());
                    queryMap.put("refundDate", queryOrder.getRefundDate());
                    queryMap.put("orderNumber", queryOrder.getOrderNumber());
                    queryMap.put("refundAccount", queryOrder.getRefundAccount());
                    queryMap.put("payAmount", queryOrder.getPayAmount());
                    queryMap.put("refundementAmount", queryOrder.getRefundementAmount());
                    queryMap.put("posCode", queryOrder.getPosCode());
                    queryMap.put("userId", queryOrder.getUserId());
                    queryMap.put("status", queryOrder.getStatus());

                    //把每一个订单信息都存放在queryOrderList中
                    queryOrderList.add(queryMap);
                }
                //把所有订单信息集合queryOrderList存放到orderMap中
                orderMap.put("queryOrderList", queryOrderList);
                logger.info("解析银行退款查询返回数据结束:" + orderMap);
                return orderMap;
            }


XStream解析XMl文件