支付宝与微信对账文件解析

一,对账文件解析

  1)在支付中会涉及到对账问题,那么对账文件解析就是个较为棘手的事情,那今天就来看下常见的对账文件格式与解析;

二,常见对账文件格式

  1)支付宝与微信的对账文件都是.csv格式,这样的对账文件可以使用exceld打开,也可以使用文本编辑器打开,

       其他类型对账文件大多是XML文件,下面来看下是什么样子的文件

    支付宝文件

    1.样式

    

    2.exceld打开样式

    3.编辑器打开样式

  微信文件

    1.文本打开样式

    

  

  XML文件

    1.XML类型文件是我们常常遇到的类型,解析方式要根据内容情况来进行

三,解析

  1)根据不同的内容设置不同的解析方法

  2)微信csv文件解析示例(支付宝同理)

//读取文件
        File file = new File("F:/test/wexin.csv");
        //解决中文乱码
        InputStreamReader isr=null;
        BufferedReader buff =null;
        try {
            isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
             buff = new BufferedReader(isr);
            String line=null;
            //数据容器
            List<WwXin> wxinlist=new ArrayList<WwXin>();
            WwXin wXin =null;
            while ((line=buff.readLine())!=null) {
                //去除`
                String replace = line.replace("`", "");
                String[] split = replace.split(",");
                //去除后两行,第一行也是不需要存到数据库的,在后续操作中去掉第一条数据
                if(split.length==24) {
                    //符合要求
                    wXin=new WwXin();
                    wXin.setTime(split[0]);
                    wXin.setId(split[1]);
                    wXin.setShh(split[2]);
                    wXin.setZshh(split[3]);
                    wXin.setSbh(split[4]);
                    wXin.setWxddh(split[5]);
                    wXin.setShddh(split[6]);
                    wXin.setYhbs(split[7]);
                    wXin.setJylx(split[8]);
                    wXin.setJyzt(split[9]);
                    wXin.setFkyh(split[10]);
                    wXin.setHbzl(split[11]);
                    wXin.setZje(split[12]);
                    wXin.setDjjlj(split[13]);
                    wXin.setWxtkdh(split[14]);
                    wXin.setHztkdh(split[15]);
                    wXin.setTkje(split[16]);
                    wXin.setLjje(split[17]);
                    wXin.setTklx(split[18]);
                    wXin.setTkzt(split[19]);
                    wXin.setSbmc(split[20]);
                    wXin.setShsjb(split[21]);
                    wXin.setSxf(split[22]);
                    wXin.setFl(split[23]);
                    //添加
                    wxinlist.add(wXin);
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(isr!=null) {
                    isr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(buff!=null) {
                    buff.close();    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

 

  3)XMl文件解析(要根据具体格式具体解析)

List<AccountQueryAccountLogVO> list = new ArrayList<AccountQueryAccountLogVO>();
        try {
            //创建SAXReader对象读取proudct4.xml文件
            SAXReader reader = new SAXReader();
            //从本地读取文件,读取xml
            File file = new File("F:/test/2017-12-26.xml");
            Document document = reader.read(file);
            //读取根节点
            Element rootElement = document.getRootElement();
            //获取全部节点
            Iterator<Element> it1 = rootElement.elementIterator();
            //遍历子节点
            while(it1.hasNext()){//判断是否有下一个子元素
                //子节点
                Element productEle = it1.next();
                Iterator<Element> it2 = productEle.elementIterator();
                //遍历product标签的子元素
                while(it2.hasNext()){
                    //获取每个子元素
                    Element eles = it2.next();
                    Iterator<Element> it3 = eles.elementIterator();
                    while(it3.hasNext()){
                        //获取每个子元素
                        Element next = it3.next();
                        Iterator<Element> it4 = next.elementIterator();
                        AccountQueryAccountLogVO accountQueryAccountLogVO =null;
                        while (it4.hasNext()) {
                            Element next2 = it4.next();
                            Iterator<Element> it5 = next2.elementIterator();
                            accountQueryAccountLogVO= new AccountQueryAccountLogVO();
                            while (it5.hasNext()) {
                                Element next3 = it5.next();
                                String name = next3.getName();
                                switch (name) {
                                case "balance":
                                    String data = (String)next3.getData();
                                    accountQueryAccountLogVO.setBalance(data);
                                    break;
                                case "buyer_account":
                                    String buyerAccount = (String)next3.getData();
                                    accountQueryAccountLogVO.setBuyerAccount(buyerAccount);
                                    break;
                                case "currency":
                                    String currency = (String)next3.getData();
                                    accountQueryAccountLogVO.setCurrency(currency);
                                    break;
                                case "deposit_bank_no":
                                    String depositBankNo = (String)next3.getData();
                                    accountQueryAccountLogVO.setDepositBankNo(depositBankNo);
                                    break;
                                case "goods_title":
                                    String goodsTitle = (String)next3.getData();
                                    accountQueryAccountLogVO.setGoodsTitle(goodsTitle);
                                    break;
                                case "income":
                                    String income = (String)next3.getData();
                                    accountQueryAccountLogVO.setIncome(income);
                                    break;
                                case "iw_account_log_id":
                                    String iwAccountLogId = (String)next3.getData();
                                    accountQueryAccountLogVO.setIwAccountLogId(iwAccountLogId);
                                    break;
                                    
                                case "merchant_out_order_no":
                                    String merchantOutOrderNo = (String)next3.getData();
                                    accountQueryAccountLogVO.setMerchantOutOrderNo(merchantOutOrderNo);
                                    break;
                                case "outcome":
                                    String outcome = (String)next3.getData();
                                    accountQueryAccountLogVO.setOutcome(outcome);
                                    break;
                                case "partner_id":
                                    String partnerId = (String)next3.getData();
                                    accountQueryAccountLogVO.setPartnerId(partnerId);
                                    break;
                                case "rate":
                                    String rate = (String)next3.getData();
                                    accountQueryAccountLogVO.setRate(rate);
                                    break;
                                case "seller_account":
                                    String sellerAccount = (String)next3.getData();
                                    accountQueryAccountLogVO.setSellerAccount(sellerAccount);
                                    break;
                                case "seller_fullname":
                                    String seller = (String)next3.getData();
                                    accountQueryAccountLogVO.setSellerAccount(seller);
                                    break;
                                case "service_fee":
                                    String serviceFee = (String)next3.getData();
                                    accountQueryAccountLogVO.setServiceFee(serviceFee);
                                    break;
                                case "sign_product_name":
                                    String signProductName = (String)next3.getData();
                                    accountQueryAccountLogVO.setSignProductName(signProductName);
                                    break;
                                case "sub_trans_code_msg":
                                    String subTransCodeMsg = (String)next3.getData();
                                    accountQueryAccountLogVO.setSubTransCodeMsg(subTransCodeMsg);
                                    break;
                                case "total_fee":
                                    String totalFee = (String)next3.getData();
                                    accountQueryAccountLogVO.setTotalFee(totalFee);
                                    break;
                                case "trade_no":
                                    String tradeNo = (String)next3.getData();
                                    accountQueryAccountLogVO.setTradeNo(tradeNo);
                                    break;
                                case "trade_refund_amount":
                                    String tradeRefundAmount = (String)next3.getData();
                                    accountQueryAccountLogVO.setTradeRefundAmount(tradeRefundAmount);
                                    break;
                                case "trans_code_msg":
                                    String transCodeMsg = (String)next3.getData();
                                    accountQueryAccountLogVO.setTransCodeMsg(transCodeMsg);;
                                    break;
                                case "trans_date":
                                    String transDate = (String)next3.getData();
                                    accountQueryAccountLogVO.setTransDate(transDate);
                                    break;
                                default:
                                    break;
                                }
                            }
                            list.add(accountQueryAccountLogVO);
                        }
                    }
                    
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
posted @ 2017-12-27 10:56  言_曦  阅读(8769)  评论(0编辑  收藏  举报