1. 程式人生 > >DOM4J對SOAP的返回資訊解析

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 = "<SOAP-ENV:Envelope\r\n" +
		"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""+
		" SOAP-ENV:encodingStyle=\"\">"+
		"<SOAP-ENV:Body>"+
		"<EAID>GameCode_ext</EAID>"+
		"<TranID>11102</TranID>"+
		"<Response>0100</Response>"+
		"</SOAP-ENV:Body>"+
		"</SOAP-ENV:Envelope>";
		
		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;
	}
	
	
	
}