解析XML报文

package com.ygsoft.gris.invoicemanage.impl.role;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import com.ygsoft.gris.invoicemanage.service.role.IGetInvoiceLimitRole;

/**
 * GetInvoiceLimitRole.<br>
 *
 * @author keyan <br>
 * @version 1.0.0 2018-7-24 14:30:03 <br>
 * @since JDK 1.5.0
 */
@Role
@RoleDesc(caption = "GetInvoiceLimitRole", type = "java")
@Component
public class GetInvoiceLimitRole implements IGetInvoiceLimitRole {

	public void getInvoiceLimit() {
		List<Map> salesList = invoiceLimitDao.getSalesInfo();
		for(int i = 0;i<salesList.size();i++){
			String requestXml = generateRequestXML(salesList.get(i).get("qysh").toString());
			LOG.info("获取单张开票限额数据请求报文xml:"+requestXml);
			String resultXml = "<?xml version=\"1.0\" encoding=\"gbk\"?>"
				+ "<business id=\"10001\" comment=\"获取监控管理数据\">"
				+ "<body><returncode>0</returncode><returnmsg>获取单张限额数据成功</returnmsg><returndata>"
				+"<dzkpxe>100000</dzkpxe></returndata></body></business>";
			Map<String, String> map = parseResultXML(resultXml);
			map.put("qysh", salesList.get(i).get("qysh").toString());
			resultList.add(map);
	}
	
	/**
	 * 组装请求报文.
	 * @param nsrsbh
	 * @return
	 */
	private String generateRequestXML(final String nsrsbh){
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"gbk\"?>");
		sb.append("<business id=\"10001\" comment=\"获取监控管理数据\">");
		sb.append("<body><nsrsbh>");
		sb.append(nsrsbh);
		sb.append("</nsrsbh></body>");
		sb.append("</business>");
		return sb.toString();
	}

	/**
	 * 解析开票返回
	 * @param newResultXML
	 * @return
	 */
	private Map<String, String> parseResultXML(final String newResultXML) {
		Map<String, String> result = new HashMap<String, String>();
		if (null != newResultXML) {
			try {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				XPath xpath = XPathFactory.newInstance().newXPath();
				// 解析文件,生成document对象
				InputSource is = new InputSource(new StringReader(newResultXML));
				Document document = builder.parse(is);
				result.put("returnCode", (String) xpath.evaluate("/business/body/returncode/text()", document, XPathConstants.STRING));
				result.put("returnMsg", (String) xpath.evaluate("/business/body/returnmsg/text()", document, XPathConstants.STRING));
				if (RESULT_SUCCESS.equals((String) result.get("returnCode"))) {
					result.put("dzkpxe", (String) xpath.evaluate("/business/body/returndata/dzkpxe/text()", document, XPathConstants.STRING));
				}
			} catch (Exception e) {
				LOG.error("解析开票返回报文异常", e);
				result.put("returnCode","-1");
				result.put("returnMsg","解析开票返回报文异常");
			}
		} else {
			result.put("returnCode","-1");
			result.put("returnMsg","接口返回报文为空");
		}
		return result;
	}
	
	
	
	

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值