Jav解析soap的xml

解析xml

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.dom4j.xpath.DefaultXPath;

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
	<soapenv:Body>
		<n:X1Response xmlns:n="http://vp.xx.com" xmlns:n0="http://vp.xx.com/xsd">
			<n:return>
				<n0:vPXResult>
					<n0:taskid>11</n0:taskid>
					<n0:vPResult>
						<n0:flag>0</n0:flag>
					</n0:vPResult>
				</n0:vPXResult>
			</n:return>
		</n:X1Response>
	</soapenv:Body>
</soapenv:Envelope>

 

 

解析代码;

 

 /**
     * 解析 返回的消息体   //后面修改为通用方法 
     * 
     * parseSoapXml:(这里用一句话描述这个方法的作用). <br/>  
     *  
     * @author   
     * @param xmlStr
     * @return
     * @throws Exception  
     * @since JDK 1.6
     * @Date:2015-4-8下午8:05:31
     */
    private boolean parseSoapXml(String xmlStr) throws Exception
    {

        Map<String, String> map = new HashMap<String, String>();

        SAXReader saxReader = new SAXReader();

        Document document = DocumentHelper.parseText(xmlStr);

        DefaultXPath xpath = new DefaultXPath("//n:return");
        xpath.setNamespaceURIs(Collections.singletonMap("n", "http://vp.xx.com"));

        List list = xpath.selectNodes(document);
        Iterator iterator = list.iterator();
        if (iterator.hasNext())
        {
            Element node = (Element) iterator.next();

            List<Element> l = node.elements();

            Element flagNode = node.element("vPX1Result").element("vPXResult").element("flag");
            if (StringUtils.equals("0", flagNode.getText()))
            {
                map.put("flag", flagNode.getText());
                return true;
            }
            else
            {
                return false;
            }
			
           // Element portNode = node.element("port");
            

            //map.put("port", portNode.getText());
  

        }

        return false;

    }

 

你可能感兴趣的:(SOAP)