我们拿支付宝对账的订单来解析。
上面的截图是,支付宝对账结果的一部分数据。下面我写下具体的代码:
private MapxmlToMap(InputStream inputStream){ Map resultMap = new HashMap (); SAXReader reader = new SAXReader(); List accountQueryAccountLogVOs = new ArrayList (); try { org.dom4j.Document doc = reader.read(inputStream); //获取alipay节点以下的所有子节点 List nodeList = doc.selectNodes("//alipay/*"); for (Node node : nodeList) { String nodeName = node.getName(); //判断是否有成功标示 if ("is_success".equals(nodeName) && node.getText().equals("T")) { //解析响应的内容信息 List 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 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;}