dom4j解析xml的简单实用

我们拿支付宝对账的订单来解析。

120848_AcHJ_1471190.png

上面的截图是,支付宝对账结果的一部分数据。下面我写下具体的代码:

private Map<String, Object> xmlToMap(InputStream inputStream){
    Map<String, Object> resultMap = new HashMap<String, Object>();
    SAXReader reader = new SAXReader();
    List<AccountQueryAccountLogVO> accountQueryAccountLogVOs = new ArrayList<AccountQueryAccountLogVO>();
    try {
        org.dom4j.Document doc = reader.read(inputStream);
        //获取alipay节点以下的所有子节点
        List<Node> nodeList = doc.selectNodes("//alipay/*");
        for (Node node : nodeList) {
            String nodeName = node.getName();
            //判断是否有成功标示
            if ("is_success".equals(nodeName) && node.getText().equals("T")) {
                //解析响应的内容信息
                List<Node> tempList = doc.selectNodes("//response/account_page_query_result/*");
                if (tempList != null && tempList.size() > 0) {
                    for (Node node1 : tempList) {
                        String tempNodeName = node1.getName();
                        String tempNodeText = node1.getText();
                        if ("has_next_page".equals(tempNodeName)) {
                            LOGGER.debug("是否有下一页:{}", "T".equals(tempNodeText) ? true : false);
                            resultMap.put("has_next_page", "T".equals(tempNodeText) ? true : false);
                        }
                        if ("page_no".equals(tempNodeName)) {
                            LOGGER.debug("当前页码:{}", tempNodeText);
                            resultMap.put("page_no", tempNodeText);
                        }
                        if ("page_size".equals(tempNodeName)) {
                            LOGGER.debug("每页显示条数:{}", tempNodeText);
                        }
                        if ("account_log_list".equals(tempNodeName)) {
                            List<Node> accountLogs = node1.selectNodes("AccountQueryAccountLogVO");
                            for (Node accountLog : accountLogs) {
                                AccountQueryAccountLogVO accountQueryAccountLogVO = new AccountQueryAccountLogVO();
                                //商户订单号
                                String merchant_out_order_no = accountLog.selectSingleNode("merchant_out_order_no").getText();
                                //支付宝交易号
                                String trade_no = accountLog.selectSingleNode("trade_no").getText();
                                //只匹配客户端充值的信息
                                if (pattern.matcher(trade_no).matches()) {
                                    //todo
                                }
                                if (StringUtils.isNoneBlank(merchant_out_order_no)) {
                                    accountQueryAccountLogVO.setMerchant_out_order_no(merchant_out_order_no);
                                }
                                if (StringUtils.isNotBlank(trade_no)) {
                                    accountQueryAccountLogVO.setTrade_no(trade_no);
                                }
                                accountQueryAccountLogVOs.add(accountQueryAccountLogVO);
                            }
                            resultMap.put("rows", accountQueryAccountLogVOs);
                        }

                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("解析遇到bug{}", e.getMessage());
    }
    LOGGER.debug("==============解析xml结束,共收集客户端账单条数:{}", accountQueryAccountLogVOs.size());
    return resultMap;
}


转载于:https://my.oschina.net/kkrgwbj/blog/653545

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值