Java解析SoapXML
import java.io.ByteArrayInputStream;
import java.util.Iterator;
l.soap.MessageFactory;
l.soap.MimeHeaders;
l.soap.SOAPBody;
l.soap.SOAPElement;
l.soap.SOAPMessage;
public class SoapUtil {
/**
* 解析soapXML
* @param soapXML
* @return
*/
public static WebserviceResultBean parseSoapMessage(String soapXML) {
WebserviceResultBean resultBean = new WebserviceResultBean();
try {
SOAPMessage msg = formatSoapString(soapXML);
SOAPBody body = SOAPBody();
Iterator<SOAPElement> iterator = ChildElements();
parse(iterator, resultBean);
} catch (Exception e) {
e.printStackTrace();
}
return resultBean;
}
public static void main(String[] args) {
String deptXML = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"/soap/envelope/\">";  deptXML += "<SOAP-ENV:Body>";
deptXML += "<ns0:GetServiceTest xmlns:ns0=\"/wsdl/test/v003\">";
deptXML += "<ns1:aa xmlns:ns1=\"/wsdl/test/v003\">";
deptXML += "<ns1:bb/>";
deptXML += "<ns1:cc>123456</ns1:cc>";
deptXML += "<ns1:dd>999</ns1:dd>";
deptXML += " <ns1:ee>123</ns1:ee>";
deptXML += "  <ns1:ff>888</ns1:ff>";
deptXML += "  <ns1:gg>666</ns1:gg>";
deptXML += "  <ns1:hh>0108A</ns1:hh>";
deptXML += " </ns1:aa>";
deptXML += "</ns0:GetServiceTest>";
deptXML += "</SOAP-ENV:Body>";
deptXML += "</SOAP-ENV:Envelope>";
WebserviceResultBean ret = parseSoapMessage(deptXML);
try {
SOAPMessage msg = formatSoapString(deptXML);
SOAPBody body = SOAPBody();
Iterator<SOAPElement> iterator = ChildElements();
PrintBody(iterator, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 把soap字符串格式化为SOAPMessage
*
* @param soapString
java xml是什么
* @return
* @see [类、类#⽅法、类#成员]
*/
public static SOAPMessage formatSoapString(String soapString) {
MessageFactory msgFactory;
try {
msgFactory = wInstance();
SOAPMessage reqMsg = ateMessage(new MimeHeaders(),
new Bytes("UTF-8")));
reqMsg.saveChanges();
return reqMsg;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解析soap xml
* @param iterator
* @param resultBean
*/
private static void parse(Iterator<SOAPElement> iterator, WebserviceResultBean resultBean) {        while (iterator.hasNext()) {
SOAPElement element = ();
if ("ns:BASEINFO".NodeName())) {
continue;
} else if ("ns:MESSAGE".NodeName())) {
Iterator<SOAPElement> it = ChildElements();
SOAPElement el = null;
while (it.hasNext()) {
el = it.next();
if ("RESULT".LocalName())) {
resultBean.Value());
System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
} else if ("REMARK".LocalName())) {
resultBean.setRemark(null != el.getValue() ? el.getValue() : "");
System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
} else if ("XMLDATA".LocalName())) {
resultBean.Value());
System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
}
}
} else if (null == Value()
&& ChildElements().hasNext()) {
ChildElements(), resultBean);
}
}
}
private static void PrintBody(Iterator<SOAPElement> iterator, String side) {
while (iterator.hasNext()) {
Object ();
if(o!=null)  {
SOAPElement element=null;
try{
element = (SOAPElement) o;
System.out.println("Node Name:" + NodeName());
System.out.println("Node Name:" + NodeName());                    System.out.println("Value:" + Value());
}catch(Exception e){}
if ( element !=null ) {
ChildElements(), side + "-----");
}
}
}
}
}
public class WebserviceResultBean{
private String result;
private String xmlData;
private String remark;
public String getResult() {
return result;
}
public void setResult(String result) {
}
public String getXmlData() {
return xmlData;
}
public void setXmlData(String xmlData) {
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。