webserivce之递归方法解析SOAP消息

本文介绍了一种使用dom4j解析SOAP消息的方法,通过递归遍历XML文件并提取关键信息到Map中,便于进一步处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("&lt;", "<");
soapData=soapData.replace("&gt;", ">");
		}
		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就是取到节点的名称和值





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值