解析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.
* * @author keyan
* @version 1.0.0 2018-7-24 14:30:03
* @since JDK 1.5.0 */ @Role @RoleDesc(caption = "GetInvoiceLimitRole", type = "java") @Component public class GetInvoiceLimitRole implements IGetInvoiceLimitRole { public void getInvoiceLimit() { List salesList = invoiceLimitDao.getSalesInfo(); for(int i = 0;i" + "" + "0获取单张限额数据成功" +"100000"; Map 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(""); sb.append(""); sb.append(""); sb.append(nsrsbh); sb.append(""); sb.append(""); return sb.toString(); } /** * 解析开票返回 * @param newResultXML * @return */ private Map parseResultXML(final String newResultXML) { Map result = new HashMap(); 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; } }

 

你可能感兴趣的:(Java)