解析XML报文

版权声明:转载本博客,请注明来源,谢谢 https://blog.csdn.net/kisorl/article/details/81204211
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;
	}
	
	
	
	

}

猜你喜欢

转载自blog.csdn.net/kisorl/article/details/81204211