DOM4J对SOAP的返回信息解析

用DOM4J的XML解析式拿不到节点的。所以网上利用DOM4J提供的VisitorSupport解决此问题。不废话,直接看代码:

 

package com.starhub.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;


public class SOAPUtil extends VisitorSupport{
	
	private String eaId;
	private String tranId;
	private String response;
	private String msisdn;
	
	public void visit(Element node) {
		
		if ("EAID".equals(node.getName())) {
			this.setEaId(node.getText());
		}else if("Response".equals(node.getName())){
			this.setResponse(node.getText());
		}else if("TranID".equals(node.getName())){
			this.setTranId(node.getText());
		}else if("MSISDN".equals(node.getName())){
			this.setMsisdn(node.getText());
		}
		
	}
	
	public static void main(String[] args) {
		String soapResponse = ""+
		""+
		"GameCode_ext"+
		"11102"+
		"0100"+
		""+
		"";
		
		SOAPUtil util = new SOAPUtil();
		
		try {
			util.analysis(soapResponse);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		System.out.println(util.getEaId());
		
		
	}
	
	public void analysis(String soapContent) throws DocumentException {
		Document doc = DocumentHelper.parseText(soapContent);
		doc.accept(this);
	}

	public String getEaId() {
		return eaId;
	}
	public void setEaId(String eaId) {
		this.eaId = eaId;
	}
	public String getTranId() {
		return tranId;
	}
	public void setTranId(String tranId) {
		this.tranId = tranId;
	}
	public String getResponse() {
		return response;
	}
	public void setResponse(String response) {
		this.response = response;
	}
	public String getMsisdn() {
		return msisdn;
	}
	public void setMsisdn(String msisdn) {
		this.msisdn = msisdn;
	}
	
	
	
}

 

参考资料:

http://www.blogjava.net/ITdavid/archive/2008/01/16/175646.html

你可能感兴趣的:(JAVA开发)