最近业务需求,需要HttpPost请求调用WebService接口,
其消息格式为String类型的SOAPXML,以下为解决方式,特此记录一下
public class SoapUtil {
13
14 /**
15 * 解析soapXML
16 * @param soapXML
17 * @return
18 */
19 public static WebserviceResultBean parseSoapMessage(String soapXML) {
20 WebserviceResultBean resultBean = new WebserviceResultBean();
21 try {
22 SOAPMessage msg = formatSoapString(soapXML);
23 SOAPBody body = msg.getSOAPBody();
24 Iterator<SOAPElement> iterator = body.getChildElements();
25 parse(iterator, resultBean);
26 } catch (Exception e) {
27 e.printStackTrace();
28 }
29 return resultBean;
30 }
31
32 public static void main(String[] args) {
33 System.out.println("开始解析soap...");
34
35 String deptXML = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP:Header/><SOAP:Body><ns:OP_SDMS_Consume_Material_SynResponse xmlns:ns=\"http://toSDMS.material.service.ffcs.com\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><ns:return><ns:BASEINFO><MSGID>?</MSGID><PMSGID>?</PMSGID><SENDTIME>20140212094609</SENDTIME><S_PROVINCE>?</S_PROVINCE><S_SYSTEM>?</S_SYSTEM><SERVICENAME>?</SERVICENAME><T_PROVINCE>?</T_PROVINCE><T_SYSTEM>?</T_SYSTEM><RETRY>?</RETRY></ns:BASEINFO><ns:MESSAGE><RESULT>E</RESULT><REMARK/><XMLDATA><![CDATA[<response><RESULT>E</RESULT><MESSAGE>平台系统处理时发生异常!保存接口接收数据出错!</MESSAGE></response>]]></XMLDATA></ns:MESSAGE></ns:return></ns:OP_SDMS_Consume_Material_SynResponse></SOAP:Body></SOAP:Envelope>";
36 WebserviceResultBean ret = parseSoapMessage(deptXML);
37 try {
38 SOAPMessage msg = formatSoapString(deptXML);
39 SOAPBody body = msg.getSOAPBody();
40 Iterator<SOAPElement> iterator = body.getChildElements();
41 PrintBody(iterator, null);
42 } catch (Exception e) {
43 e.printStackTrace();
44 }
45
46 System.out.println("解析soap结束...");
47 }
48
49 /**
50 * 把soap字符串格式化为SOAPMessage
51 *
52 * @param soapString
53 * @return
54 * @see [类、类#方法、类#成员]
55 */