|
用DOM4J的XML解析是拿不到节点的。所以网上利用DOM4J提供的VisitorSupport解决此问题。不废话,直接看代码:
package com.infindo.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;
}
} |
|