目标
解析 SOAP 数据格式
开发环境
| 名称 | 版本 |
|---|---|
| 操作系统 | Windows 10 X64 |
| JDK | JDK1.8(jdk-8u151-windows-x64) |
| IntelliJ IDEA | IntelliJ IDEA 2018.3 |
| Maven | Maven 3.6.0 |
SOAP 解析参考
说明
用 Java 程序实现下面的接口调用


Headers 参数
| 名称 | 值 |
|---|---|
| SOAPAction | |
| Content-Type | text/xml; charset=utf-8 |
Body 参数
<?xml version="1.0" encoding="utf-16"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><PGetPo xmlns="http://tempuri.org/"><startDate></startDate><endDate></endDate><appKey>luoma</appKey></PGetPo></soap:Body></soap:Envelope>
返回值
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><PGetPoResponse xmlns="http://tempuri.org/"><PGetPoResult>{"code":0,"data":[{"PO_NUMBER":"POGO201702170002","ORG_ID":115.0,"VENDOR_AREA":"境外","VENDOR_NAME":"xx1有限公司","VENDOR_ID":16684.0,"CONTRACT_CODE":"T-044-PUR-201510723-03","APPLY_NO":"PA-PL-201703220001","CREATION_DATE":"\/Date(1487294589000+0800)\/"},{"PO_NUMBER":"POGO201702200016","ORG_ID":115.0,"VENDOR_AREA":"境外","VENDOR_NAME":"xx2有限公司","VENDOR_ID":3331207.0,"CONTRACT_CODE":"T-044-PUR-20150602-01","APPLY_NO":"PA-PL-201703230001","CREATION_DATE":"\/Date(1487575960000+0800)\/"}]}</PGetPoResult></PGetPoResponse></soap:Body></soap:Envelope>
公共类-WebClientUtils
公共类-SoapUtil
package com.foreign.payment.common.util;import com.alibaba.fastjson.JSON;import com.foreign.payment.common.data.PGetPoResult;import com.foreign.payment.common.data.WebserviceResultBean;import java.io.ByteArrayInputStream;import java.util.Iterator;import javax.xml.soap.MessageFactory;import javax.xml.soap.MimeHeaders;import javax.xml.soap.SOAPBody;import javax.xml.soap.SOAPElement;import javax.xml.soap.SOAPMessage;/*** XML Soap 解析* https://blog.csdn.net/chenmintong/article/details/79970624*/public class SoapUtil {/*** 解析soapXML* @param soapXML* @return*/public static WebserviceResultBean parseSoapMessage(String soapXML) {WebserviceResultBean resultBean = new WebserviceResultBean();try {SOAPMessage msg = formatSoapString(soapXML);SOAPBody body = msg.getSOAPBody();Iterator<SOAPElement> iterator = body.getChildElements();parse(iterator, resultBean);} catch (Exception e) {e.printStackTrace();}return resultBean;}public static void main(String[] args) {String deptXML = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">";deptXML += "<SOAP-ENV:Body>";deptXML += "<ns0:GetServiceTest xmlns:ns0=\"http://www.xxx.com/wsdl/test/v003\">";deptXML += "<ns1:aa xmlns:ns1=\"http://www.xxx.com/wsdl/test/v003\">";deptXML += "<ns1:bb/>";deptXML += "<ns1:cc>123456</ns1:cc>";deptXML += "<ns1:dd>999</ns1:dd>";deptXML += " <ns1:ee>123</ns1:ee>";deptXML += " <ns1:ff>888</ns1:ff>";deptXML += " <ns1:gg>666</ns1:gg>";deptXML += " <ns1:hh>0108A</ns1:hh>";deptXML += " </ns1:aa>";deptXML += "</ns0:GetServiceTest>";deptXML += "</SOAP-ENV:Body>";deptXML += "</SOAP-ENV:Envelope>";deptXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";deptXML += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";deptXML += "<soap:Body>";deptXML += "<PGetPoResponse xmlns=\"http://tempuri.org/\">";deptXML += "<PGetPoResult>{\"code\":0,\"data\":[{\"PO_NUMBER\":\"POGO201702200016\",\"ORG_ID\":115.0,\"VENDOR_AREA\":\"境外\",\"VENDOR_NAME\":\"xx1有限公司\",\"CONTRACT_CODE\":\"T-044-PUR-20150602-01\",\"APPLY_NO\":\"PA-PL-201703230001\",\"CREATION_DATE\":\"\\/Date(1487575960000+0800)\\/\"},{\"PO_NUMBER\":\"POGO201702170002\",\"ORG_ID\":115.0,\"VENDOR_AREA\":\"境外\",\"VENDOR_NAME\":\"xx2有限公司\",\"CONTRACT_CODE\":\"T-044-PUR-20150723-03\",\"APPLY_NO\":\"PA-PL-201703220001\",\"CREATION_DATE\":\"\\/Date(1487294589000+0800)\\/\"}]}</PGetPoResult>\n";deptXML += "</PGetPoResponse>";deptXML += "</soap:Body>";deptXML += "</soap:Envelope>";//WebserviceResultBean ret = parseSoapMessage(deptXML);try {SOAPMessage msg = formatSoapString(deptXML);SOAPBody body = msg.getSOAPBody();Iterator<SOAPElement> iterator = body.getChildElements();PrintBody(iterator, null);} catch (Exception e) {e.printStackTrace();}}/*** 把soap字符串格式化为SOAPMessage** @param soapString* @return* @see [类、类#方法、类#成员]*/public static SOAPMessage formatSoapString(String soapString) {MessageFactory msgFactory;try {msgFactory = MessageFactory.newInstance();SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),new ByteArrayInputStream(soapString.getBytes("UTF-8")));reqMsg.saveChanges();return reqMsg;} catch (Exception e) {e.printStackTrace();return null;}}/*** 解析soap xml* @param iterator* @param resultBean*/private static void parse(Iterator<SOAPElement> iterator, WebserviceResultBean resultBean) {while (iterator.hasNext()) {SOAPElement element = iterator.next();//nsif ("PGetPo:BASEINFO".equals(element.getNodeName())) {continue;} else if ("PGetPo:MESSAGE".equals(element.getNodeName())) {//nsIterator<SOAPElement> it = element.getChildElements();SOAPElement el = null;while (it.hasNext()) {el = it.next();if ("RESULT".equals(el.getLocalName())) {resultBean.setResult(el.getValue());System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue());} else if ("REMARK".equals(el.getLocalName())) {resultBean.setRemark(null != el.getValue() ? el.getValue() : "");System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue());} else if ("XMLDATA".equals(el.getLocalName())) {resultBean.setXmlData(el.getValue());System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue());}}} else if (null == element.getValue()&& element.getChildElements().hasNext()) {parse(element.getChildElements(), resultBean);}}}public static void PrintBody(Iterator<SOAPElement> iterator, String side) {PGetPoResult result =new PGetPoResult();while (iterator.hasNext()) {Object o=iterator.next();if(o!=null) {SOAPElement element=null;try{element = (SOAPElement) o;System.out.println("Node Name:" + element.getNodeName());System.out.println("Value:" + element.getValue());if(element.getNodeName().equals("PGetPoResult")){String nodeValue = element.getValue();result = JSON.parseObject(nodeValue, PGetPoResult.class);System.out.println(result.getCode());System.out.println(result.getData().get(0).getAPPLY_NO());//return result;}}catch(Exception e){}if ( element !=null ) {PrintBody(element.getChildElements(), side + "-----");}}}//return result;}}
解析类-PGetPoResult
package com.foreign.payment.common.data;import lombok.Data;import java.util.Date;import java.util.List;/*** 实物进口供应商解析类** @author: v_hwhao* @date: 2020-02-11 12:21*/@Datapublic class PGetPoResult {String code;List<PGetPoItem> data;@Datapublic class PGetPoItem{/*** EPO 订单号*/String PO_NUMBER;/*** 我方主体ID*/String ORG_ID;/*** 供应商区域*/String VENDOR_AREA;/*** 供应商名称*/String VENDOR_NAME;/*** 合同号*/String CONTRACT_CODE;/*** 付款单号*/String APPLY_NO;/*** EPO 订单创建时间*/Date CREATION_DATE;}}
解析类-WebserviceResultBean
package com.foreign.payment.common.data;public class WebserviceResultBean{private String result;private String xmlData;private String remark;public String getResult() {return result;}public void setResult(String result) {this.result = result;}public String getXmlData() {return xmlData;}public void setXmlData(String xmlData) {this.xmlData = xmlData;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}}
调用解析逻辑
/*** 调用接口获取实物进口供应商数据* @return 实物进口供应商数据*/public PGetPoResult getSupplierPhysical(){PGetPoResult result = new PGetPoResult();//soap 请求参数StringBuffer sb = new StringBuffer("");sb.append("<?xml version=\"1.0\" encoding=\"utf-16\"?>");sb.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");sb.append("<soap:Body>");sb.append("<PGetPo xmlns=\"http://tempuri.org/\">");sb.append("<startDate></startDate>");sb.append("<endDate></endDate>");sb.append("<appKey>"+config.getSupplierPhysicalAppId()+"</appKey>");sb.append("</PGetPo>");sb.append("</soap:Body>");sb.append("</soap:Envelope>");//headerMap<String,String> headers = new HashMap<>();headers.put("SOAPAction","");//headers.put("Content-Type","text/xml; charset=utf-8");//调用接口获取 SOAP 数据String resultStr = WebClientUtils.post(config.getSupplierPhysicalUrl(), headers,sb.toString(),"text/xml; charset=utf-8");//result.setCode(resultStr);try {SOAPMessage msg = SoapUtil.formatSoapString(resultStr);SOAPBody body = msg.getSOAPBody();Iterator<SOAPElement> iterator = body.getChildElements();result = getPGetPoResult(iterator,null);} catch (Exception e) {e.printStackTrace();}return result;}/*** 解析 SOAP 数据为 PGetPoResult* @param iterator Iterator<SOAPElement>* @param side* @return PGetPoResult*/public static PGetPoResult getPGetPoResult(Iterator<SOAPElement> iterator, String side) {PGetPoResult result =new PGetPoResult();while (iterator.hasNext()) {Object o=iterator.next();if(o!=null) {SOAPElement element=null;try{element = (SOAPElement) o;System.out.println("Node Name:" + element.getNodeName());System.out.println("Value:" + element.getValue());if(element.getNodeName().equals("PGetPoResult")){String nodeValue = element.getValue();result = JSON.parseObject(nodeValue, PGetPoResult.class);System.out.println(result.getCode());System.out.println(result.getData().get(0).getAPPLY_NO());break;//return result;}}catch(Exception e){}if ( element !=null ) {result = getPGetPoResult(element.getChildElements(), side + "-----");}}}return result;}
测试结果
调用方法 getSupplierPhysical(),返回结果为
{"code": "0","data": [{"apply_NO": "PA-PL-201703220001","po_NUMBER": "POGO201702170002","vendor_NAME": "xx1有限公司","creation_DATE": "2017-02-17T01:23:09.000+0000","contract_CODE": "T-044-PUR-20150723-03","org_ID": "115.0","vendor_AREA": "境外"},{"apply_NO": "PA-PL-201703230001","po_NUMBER": "POGO201702200016","vendor_NAME": "xx2有限公司","creation_DATE": "2017-02-20T07:32:40.000+0000","contract_CODE": "T-044-PUR-20150602-01","org_ID": "115.0","vendor_AREA": "境外"}]}