SOAP消息还是本质还是XML文件,所以我解析SOAP消息使用的是dom4j,如下
package com.tool;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* SOAP消息转码
* @author wyf
*
*/
public class ReadSoapUnit {
private static Map<String,String> map = new HashMap<String,String>();
public static String changeToHtml(String soapData){
if(soapData!=null && !"".equals(soapData)){
soapData=soapData.replace("<", "<");
soapData=soapData.replace(">", ">");
}
return soapData;
}
/**
* 解析 SOAP 消息 XML文件 递归
* @param element
* @author wyf
*/
public static Map<String,String> readSoap(Element element){
if(element.elements().size()==0){
System.out.println(element.getName()+"="+element.getText());
return null;
}
for (Iterator<Element> iter=element.elementIterator();iter.hasNext();) {
Element ele=iter.next();
if(ele.getParent().getName().equalsIgnoreCase("BODY")){
System.out.println("body的子元素:"+ele.getName());
}
System.out.println(ele.getName()+"="+ele.getText().trim());
if(ele.elements().size()!=0){
readSoap(ele);
}
if(!"".equals(ele.getText().trim())&&ele.getText()!=null){
map.put(ele.getName(), ele.getText());
}
}
return map;
}
/**
* @param args
*/
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer();
sb1.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:operReqResponse xmlns:ns2=\"http://user.gxgd.com/UserCenter\"><resultType>RSP_SSO</resultType><responseSysCode>21000</responseSysCode><operResponseXML><UAPRoot><SessionHeader><ActionCode>1</ActionCode><TransactionID>210012014052220140522538</TransactionID><RspTime>2014-06-11 11:08:50</RspTime></SessionHeader><SessionBody><AssertionQueryResp><Assertion><AssertionID>76EE77C83FBD54E5EB3F742F25E2699D</AssertionID><Issuer><UCID></UCID><NotBefore>2014-06-11 10:50:37</NotBefore><NotOnOrAfter>2014-06-11 11:20:37</NotOnOrAfter></Issuer><IssueInstant>2014-06-11 11:08:50</IssueInstant><AudienceID></AudienceID><AssertionStatement><AuthInstant>2014-06-11 10:50:37</AuthInstant><SSOMethod></SSOMethod><AccountID>18978907368</AccountID><SSOFrom>21006</SSOFrom><AccountState></AccountState><PassworType>20</PassworType><BindList/><CustInfo><CustName>用户中心</CustName><CertyType>1</CertyType><CertyCode>UC</CertyCode><Mobile>null</Mobile><eMail>admin@uc.com</eMail><Address></Address><Notes></Notes><QuesionCode></QuesionCode><QuesionContent></QuesionContent><Answer></Answer></CustInfo></AssertionStatement></Assertion></AssertionQueryResp></SessionBody></UAPRoot></operResponseXML><digitalSign></digitalSign></ns2:operReqResponse></soap:Body></soap:Envelope>");
System.out.println("转换前:"+sb1.toString());
String resultStr = changeToHtml(sb1.toString());
System.out.println("转换后:"+resultStr);
try {
//转换成XML格式
Document doc = DocumentHelper.parseText(resultStr);
Element element = doc.getRootElement();
System.out.println(element.getName());
System.out.println(element.getText());
Map<String,String> map = readSoap(element);
System.out.println("map:"+map);
//获取节点名称
//String CustName = e.elementTextTrim("qqCode");
//System.out.println(CustName);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
因为我读取的消息存在转码,所以做了简单的转码
readSoap方法返回的MAP就是取到节点的名称和值