2017-05-30 106 views
0

獲得價值我得到下面的SOAP響應從Java SOAP響應

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <ns0:Get_Response xmlns:ns0="urn:DAL:OrderShim_WS"> 
     <ns0:Order_Number>Order001165</ns0:Order_Number> 
     </ns0:Get_Response> 
    </soapenv:Body> 
</soapenv:Envelope> 

我需要從上述反應得到Order_Number。爲此我寫下面的代碼

import javax.xml.soap.MessageFactory; 
import javax.xml.soap.MimeHeaders; 
import javax.xml.soap.SOAPBody; 
import javax.xml.soap.SOAPConnection; 
import javax.xml.soap.SOAPConnectionFactory; 
import javax.xml.soap.SOAPElement; 
import javax.xml.soap.SOAPEnvelope; 
import javax.xml.soap.SOAPHeader; 
import javax.xml.soap.SOAPMessage; 
import javax.xml.soap.SOAPPart; 

public class Test { 
    public static void main(String[] args) { 
     try { 
      SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
      SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 
      String url = "http://devlocal:8080/arsys/services/"; 
      SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 
      soapResponse.writeTo(System.out); 

      soapConnection.close(); 
     } catch (Exception e) { 
      System.out.println("Exception : " + e); 
     } 
    } 
} 

我能夠得到響應。但是我怎樣才能得到Order_Number的價值。

我正在使用Java。

+0

您正在使用的soapUI? – Rao

回答

0

你應該檢索SOAPBody對象和迭代的節點是這樣的:

SOAPBody body = soapResponse .getSOAPBody(); 

NodeList returnList = body.getElementsByTagName("YOUR_TAG"); 
for (int k = 0; k < returnList.getLength(); k++) { 
    NodeList innerResultList = returnList.item(k).getChildNodes(); 
    // processing nodes 
    } 
} 
+0

我應該寫「ns0:Order_Number」來代替「YOUR_TAG」嗎? – user3441151