Java解析Soap XML
1 package com.jstrd.tipstock.webservice.jt.base; 2 3 import java.io.ByteArrayInputStream; 4 import java.util.Iterator; 5 6 import javax.xml.soap.MessageFactory; 7 import javax.xml.soap.MimeHeaders; 8 import javax.xml.soap.SOAPBody; 9 import javax.xml.soap.SOAPElement; 10 import javax.xml.soap.SOAPMessage; 11 12 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 */ 56 private static SOAPMessage formatSoapString(String soapString) { 57 MessageFactory msgFactory; 58 try { 59 msgFactory = MessageFactory.newInstance(); 60 SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(), 61 new ByteArrayInputStream(soapString.getBytes("UTF-8"))); 62 reqMsg.saveChanges(); 63 return reqMsg; 64 } catch (Exception e) { 65 e.printStackTrace(); 66 return null; 67 } 68 } 69 70 /** 71 * 解析soap xml 72 * @param iterator 73 * @param resultBean 74 */ 75 private static void parse(Iterator<SOAPElement> iterator, WebserviceResultBean resultBean) { 76 while (iterator.hasNext()) { 77 SOAPElement element = iterator.next(); 78 // System.out.println("Local Name:" + element.getLocalName()); 79 // System.out.println("Node Name:" + element.getNodeName()); 80 // System.out.println("Tag Name:" + element.getTagName()); 81 // System.out.println("Value:" + element.getValue()); 82 if ("ns:BASEINFO".equals(element.getNodeName())) { 83 continue; 84 } else if ("ns:MESSAGE".equals(element.getNodeName())) { 85 Iterator<SOAPElement> it = element.getChildElements(); 86 SOAPElement el = null; 87 while (it.hasNext()) { 88 el = it.next(); 89 if ("RESULT".equals(el.getLocalName())) { 90 resultBean.setResult(el.getValue()); 91 System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue()); 92 } else if ("REMARK".equals(el.getLocalName())) { 93 resultBean.setRemark(null != el.getValue() ? el.getValue() : ""); 94 System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue()); 95 } else if ("XMLDATA".equals(el.getLocalName())) { 96 resultBean.setXmlData(el.getValue()); 97 System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue()); 98 } 99 } 100 } else if (null == element.getValue() 101 && element.getChildElements().hasNext()) { 102 parse(element.getChildElements(), resultBean); 103 } 104 } 105 } 106 107 private static void PrintBody(Iterator<SOAPElement> iterator, String side) { 108 while (iterator.hasNext()) { 109 SOAPElement element = (SOAPElement) iterator.next(); 110 System.out.println("Local Name:" + element.getLocalName()); 111 System.out.println("Node Name:" + element.getNodeName()); 112 System.out.println("Tag Name:" + element.getTagName()); 113 System.out.println("Value:" + element.getValue()); 114 if (null == element.getValue() 115 && element.getChildElements().hasNext()) { 116 PrintBody(element.getChildElements(), side + "-----"); 117 } 118 } 119 } 120 }