dom4j解析xml的简单实用

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

dom4j解析xml的简单实用_第1张图片

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

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;
}


你可能感兴趣的:(sax,dom4j)