Java解析Soap XML
import java.io.ByteArrayInputStream;
import java.util.Iterator;


import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.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 = msg.getSOAPBody();
            Iterator<SOAPElement> iterator = body.getChildElements();
            parse(iterator, resultBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultBean;
    }


    public static void main(String[] args) {
		String deptXML = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		deptXML += "<SOAP-ENV:Body>";
		deptXML += "<ns0:GetServiceTest xmlns:ns0=\"http://www.xxx.com/wsdl/test/v003\">";
		deptXML += "<ns1:aa xmlns:ns1=\"http://www.xxx.com/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 = msg.getSOAPBody();
            Iterator<SOAPElement> iterator = body.getChildElements();
            PrintBody(iterator, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 把soap字符串格式化为SOAPMessage
     *
     * @param soapString
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static SOAPMessage formatSoapString(String soapString) {
        MessageFactory msgFactory;
        try {
            msgFactory = MessageFactory.newInstance();
            SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
                    new ByteArrayInputStream(soapString.getBytes("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 = iterator.next();
            if ("ns:BASEINFO".equals(element.getNodeName())) {
                continue;
            } else if ("ns:MESSAGE".equals(element.getNodeName())) {
                Iterator<SOAPElement> it = element.getChildElements();
                SOAPElement el = null;
                while (it.hasNext()) {
                    el = it.next();
                    if ("RESULT".equals(el.getLocalName())) {
                        resultBean.setResult(el.getValue());
                        System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
                    } else if ("REMARK".equals(el.getLocalName())) {
                        resultBean.setRemark(null != el.getValue() ? el.getValue() : "");
                        System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
                    } else if ("XMLDATA".equals(el.getLocalName())) {
                        resultBean.setXmlData(el.getValue());
                        System.out.println("#### " + el.getLocalName() + "  ====  " + el.getValue());
                    }
                }
            } else if (null == element.getValue()
                    && element.getChildElements().hasNext()) {
                parse(element.getChildElements(), resultBean);
            }
        }
    }


    private static void PrintBody(Iterator<SOAPElement> iterator, String side) {
        while (iterator.hasNext()) {
        	Object o=iterator.next();
        	if(o!=null)  {
        		SOAPElement element=null;
        		try{
                    element = (SOAPElement) o;
                    System.out.println("Node Name:" + element.getNodeName());
                    System.out.println("Value:" + element.getValue());
        		}catch(Exception e){}
                if ( element !=null ) {
                    PrintBody(element.getChildElements(), side + "-----");
                }
        	}
        }
    }
}
public class WebserviceResultBean{
 
	private String result;
	private String xmlData;
	private String remark;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getXmlData() {
		return xmlData;
	}

	public void setXmlData(String xmlData) {
		this.xmlData = xmlData;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
}


            
阅读更多
个人分类: java
想对作者说点什么? 我来说一句

SOAP xml解析

2014年03月26日 13KB 下载

没有更多推荐了,返回首页

不良信息举报

Java解析Soap XML

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭