Java-解析 SOAP 格式数据总结,内部类使用

2020年02月12日 18:19 · 阅读(2819) ·

目标

解析 SOAP 数据格式

开发环境

名称 版本
操作系统 Windows 10 X64
JDK JDK1.8(jdk-8u151-windows-x64)
IntelliJ IDEA IntelliJ IDEA 2018.3
Maven Maven 3.6.0

SOAP 解析参考

Java解析Soap XML

说明

用 Java 程序实现下面的接口调用

Headers 参数

名称
SOAPAction
Content-Type text/xml; charset=utf-8

Body 参数

  1. <?xml version="1.0" encoding="utf-16"?>
  2. <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">
  3. <soap:Body>
  4. <PGetPo xmlns="http://tempuri.org/">
  5. <startDate></startDate>
  6. <endDate></endDate>
  7. <appKey>luoma</appKey>
  8. </PGetPo>
  9. </soap:Body>
  10. </soap:Envelope>

返回值

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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">
  3. <soap:Body>
  4. <PGetPoResponse xmlns="http://tempuri.org/">
  5. <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>
  6. </PGetPoResponse>
  7. </soap:Body>
  8. </soap:Envelope>

公共类-WebClientUtils

公共类-WebClientUtils

公共类-SoapUtil

  1. package com.foreign.payment.common.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.foreign.payment.common.data.PGetPoResult;
  4. import com.foreign.payment.common.data.WebserviceResultBean;
  5. import java.io.ByteArrayInputStream;
  6. import java.util.Iterator;
  7. import javax.xml.soap.MessageFactory;
  8. import javax.xml.soap.MimeHeaders;
  9. import javax.xml.soap.SOAPBody;
  10. import javax.xml.soap.SOAPElement;
  11. import javax.xml.soap.SOAPMessage;
  12. /**
  13. * XML Soap 解析
  14. * https://blog.csdn.net/chenmintong/article/details/79970624
  15. */
  16. public class SoapUtil {
  17. /**
  18. * 解析soapXML
  19. * @param soapXML
  20. * @return
  21. */
  22. public static WebserviceResultBean parseSoapMessage(String soapXML) {
  23. WebserviceResultBean resultBean = new WebserviceResultBean();
  24. try {
  25. SOAPMessage msg = formatSoapString(soapXML);
  26. SOAPBody body = msg.getSOAPBody();
  27. Iterator<SOAPElement> iterator = body.getChildElements();
  28. parse(iterator, resultBean);
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. return resultBean;
  33. }
  34. public static void main(String[] args) {
  35. String deptXML = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">";
  36. deptXML += "<SOAP-ENV:Body>";
  37. deptXML += "<ns0:GetServiceTest xmlns:ns0=\"http://www.xxx.com/wsdl/test/v003\">";
  38. deptXML += "<ns1:aa xmlns:ns1=\"http://www.xxx.com/wsdl/test/v003\">";
  39. deptXML += "<ns1:bb/>";
  40. deptXML += "<ns1:cc>123456</ns1:cc>";
  41. deptXML += "<ns1:dd>999</ns1:dd>";
  42. deptXML += " <ns1:ee>123</ns1:ee>";
  43. deptXML += " <ns1:ff>888</ns1:ff>";
  44. deptXML += " <ns1:gg>666</ns1:gg>";
  45. deptXML += " <ns1:hh>0108A</ns1:hh>";
  46. deptXML += " </ns1:aa>";
  47. deptXML += "</ns0:GetServiceTest>";
  48. deptXML += "</SOAP-ENV:Body>";
  49. deptXML += "</SOAP-ENV:Envelope>";
  50. deptXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  51. 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\">";
  52. deptXML += "<soap:Body>";
  53. deptXML += "<PGetPoResponse xmlns=\"http://tempuri.org/\">";
  54. 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";
  55. deptXML += "</PGetPoResponse>";
  56. deptXML += "</soap:Body>";
  57. deptXML += "</soap:Envelope>";
  58. //WebserviceResultBean ret = parseSoapMessage(deptXML);
  59. try {
  60. SOAPMessage msg = formatSoapString(deptXML);
  61. SOAPBody body = msg.getSOAPBody();
  62. Iterator<SOAPElement> iterator = body.getChildElements();
  63. PrintBody(iterator, null);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. /**
  69. * 把soap字符串格式化为SOAPMessage
  70. *
  71. * @param soapString
  72. * @return
  73. * @see [类、类#方法、类#成员]
  74. */
  75. public static SOAPMessage formatSoapString(String soapString) {
  76. MessageFactory msgFactory;
  77. try {
  78. msgFactory = MessageFactory.newInstance();
  79. SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
  80. new ByteArrayInputStream(soapString.getBytes("UTF-8")));
  81. reqMsg.saveChanges();
  82. return reqMsg;
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. return null;
  86. }
  87. }
  88. /**
  89. * 解析soap xml
  90. * @param iterator
  91. * @param resultBean
  92. */
  93. private static void parse(Iterator<SOAPElement> iterator, WebserviceResultBean resultBean) {
  94. while (iterator.hasNext()) {
  95. SOAPElement element = iterator.next();
  96. //ns
  97. if ("PGetPo:BASEINFO".equals(element.getNodeName())) {
  98. continue;
  99. } else if ("PGetPo:MESSAGE".equals(element.getNodeName())) {//ns
  100. Iterator<SOAPElement> it = element.getChildElements();
  101. SOAPElement el = null;
  102. while (it.hasNext()) {
  103. el = it.next();
  104. if ("RESULT".equals(el.getLocalName())) {
  105. resultBean.setResult(el.getValue());
  106. System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue());
  107. } else if ("REMARK".equals(el.getLocalName())) {
  108. resultBean.setRemark(null != el.getValue() ? el.getValue() : "");
  109. System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue());
  110. } else if ("XMLDATA".equals(el.getLocalName())) {
  111. resultBean.setXmlData(el.getValue());
  112. System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue());
  113. }
  114. }
  115. } else if (null == element.getValue()
  116. && element.getChildElements().hasNext()) {
  117. parse(element.getChildElements(), resultBean);
  118. }
  119. }
  120. }
  121. public static void PrintBody(Iterator<SOAPElement> iterator, String side) {
  122. PGetPoResult result =new PGetPoResult();
  123. while (iterator.hasNext()) {
  124. Object o=iterator.next();
  125. if(o!=null) {
  126. SOAPElement element=null;
  127. try{
  128. element = (SOAPElement) o;
  129. System.out.println("Node Name:" + element.getNodeName());
  130. System.out.println("Value:" + element.getValue());
  131. if(element.getNodeName().equals("PGetPoResult")){
  132. String nodeValue = element.getValue();
  133. result = JSON.parseObject(nodeValue, PGetPoResult.class);
  134. System.out.println(result.getCode());
  135. System.out.println(result.getData().get(0).getAPPLY_NO());
  136. //return result;
  137. }
  138. }catch(Exception e){}
  139. if ( element !=null ) {
  140. PrintBody(element.getChildElements(), side + "-----");
  141. }
  142. }
  143. }
  144. //return result;
  145. }
  146. }

解析类-PGetPoResult

  1. package com.foreign.payment.common.data;
  2. import lombok.Data;
  3. import java.util.Date;
  4. import java.util.List;
  5. /**
  6. * 实物进口供应商解析类
  7. *
  8. * @author: v_hwhao
  9. * @date: 2020-02-11 12:21
  10. */
  11. @Data
  12. public class PGetPoResult {
  13. String code;
  14. List<PGetPoItem> data;
  15. @Data
  16. public class PGetPoItem{
  17. /**
  18. * EPO 订单号
  19. */
  20. String PO_NUMBER;
  21. /**
  22. * 我方主体ID
  23. */
  24. String ORG_ID;
  25. /**
  26. * 供应商区域
  27. */
  28. String VENDOR_AREA;
  29. /**
  30. * 供应商名称
  31. */
  32. String VENDOR_NAME;
  33. /**
  34. * 合同号
  35. */
  36. String CONTRACT_CODE;
  37. /**
  38. * 付款单号
  39. */
  40. String APPLY_NO;
  41. /**
  42. * EPO 订单创建时间
  43. */
  44. Date CREATION_DATE;
  45. }
  46. }

解析类-WebserviceResultBean

  1. package com.foreign.payment.common.data;
  2. public class WebserviceResultBean{
  3. private String result;
  4. private String xmlData;
  5. private String remark;
  6. public String getResult() {
  7. return result;
  8. }
  9. public void setResult(String result) {
  10. this.result = result;
  11. }
  12. public String getXmlData() {
  13. return xmlData;
  14. }
  15. public void setXmlData(String xmlData) {
  16. this.xmlData = xmlData;
  17. }
  18. public String getRemark() {
  19. return remark;
  20. }
  21. public void setRemark(String remark) {
  22. this.remark = remark;
  23. }
  24. }

调用解析逻辑

  1. /**
  2. * 调用接口获取实物进口供应商数据
  3. * @return 实物进口供应商数据
  4. */
  5. public PGetPoResult getSupplierPhysical(){
  6. PGetPoResult result = new PGetPoResult();
  7. //soap 请求参数
  8. StringBuffer sb = new StringBuffer("");
  9. sb.append("<?xml version=\"1.0\" encoding=\"utf-16\"?>");
  10. 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\">");
  11. sb.append("<soap:Body>");
  12. sb.append("<PGetPo xmlns=\"http://tempuri.org/\">");
  13. sb.append("<startDate></startDate>");
  14. sb.append("<endDate></endDate>");
  15. sb.append("<appKey>"+config.getSupplierPhysicalAppId()+"</appKey>");
  16. sb.append("</PGetPo>");
  17. sb.append("</soap:Body>");
  18. sb.append("</soap:Envelope>");
  19. //header
  20. Map<String,String> headers = new HashMap<>();
  21. headers.put("SOAPAction","");
  22. //headers.put("Content-Type","text/xml; charset=utf-8");
  23. //调用接口获取 SOAP 数据
  24. String resultStr = WebClientUtils.post(config.getSupplierPhysicalUrl(), headers,sb.toString(),"text/xml; charset=utf-8");
  25. //result.setCode(resultStr);
  26. try {
  27. SOAPMessage msg = SoapUtil.formatSoapString(resultStr);
  28. SOAPBody body = msg.getSOAPBody();
  29. Iterator<SOAPElement> iterator = body.getChildElements();
  30. result = getPGetPoResult(iterator,null);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return result;
  35. }
  36. /**
  37. * 解析 SOAP 数据为 PGetPoResult
  38. * @param iterator Iterator<SOAPElement>
  39. * @param side
  40. * @return PGetPoResult
  41. */
  42. public static PGetPoResult getPGetPoResult(Iterator<SOAPElement> iterator, String side) {
  43. PGetPoResult result =new PGetPoResult();
  44. while (iterator.hasNext()) {
  45. Object o=iterator.next();
  46. if(o!=null) {
  47. SOAPElement element=null;
  48. try{
  49. element = (SOAPElement) o;
  50. System.out.println("Node Name:" + element.getNodeName());
  51. System.out.println("Value:" + element.getValue());
  52. if(element.getNodeName().equals("PGetPoResult")){
  53. String nodeValue = element.getValue();
  54. result = JSON.parseObject(nodeValue, PGetPoResult.class);
  55. System.out.println(result.getCode());
  56. System.out.println(result.getData().get(0).getAPPLY_NO());
  57. break;
  58. //return result;
  59. }
  60. }catch(Exception e){}
  61. if ( element !=null ) {
  62. result = getPGetPoResult(element.getChildElements(), side + "-----");
  63. }
  64. }
  65. }
  66. return result;
  67. }

测试结果

调用方法 getSupplierPhysical(),返回结果为

  1. {
  2. "code": "0",
  3. "data": [
  4. {
  5. "apply_NO": "PA-PL-201703220001",
  6. "po_NUMBER": "POGO201702170002",
  7. "vendor_NAME": "xx1有限公司",
  8. "creation_DATE": "2017-02-17T01:23:09.000+0000",
  9. "contract_CODE": "T-044-PUR-20150723-03",
  10. "org_ID": "115.0",
  11. "vendor_AREA": "境外"
  12. },
  13. {
  14. "apply_NO": "PA-PL-201703230001",
  15. "po_NUMBER": "POGO201702200016",
  16. "vendor_NAME": "xx2有限公司",
  17. "creation_DATE": "2017-02-20T07:32:40.000+0000",
  18. "contract_CODE": "T-044-PUR-20150602-01",
  19. "org_ID": "115.0",
  20. "vendor_AREA": "境外"
  21. }
  22. ]
  23. }