2013-12-09 82 views
10

我試圖從我從webservice得到的響應中獲取特定值。不幸的是我不知道該怎麼做。我用計算器發現代碼創建SOAP請求,並寫出響應內容到標準輸出:Java肥皂請求 - 閱讀肥皂響應

private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 
    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
    Source sourceContent = soapResponse.getSOAPPart().getContent(); 
    System.out.print("\nResponse SOAP Message = "); 
    StreamResult result = new StreamResult(System.out); 
    transformer.transform(sourceContent, result); 
} 

這一切運作良好,但我不需要整個響應內容:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bin="http://localhost/WebService/bindings" xmlns:typ="http://localhost/WebService/types"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <bin:doActionResponse> 
     <bin:out> 
      <typ:result> 
       <typ:code>?</typ:code> 
       <typ:description>?</typ:description> 
      </typ:result> 
     </bin:out> 
     </bin:doActionResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我只是需要的代碼值以及這個迴應的描述。我怎樣才能做到這一點?

+0

看看XPATH如果你試圖從肥皂消息中獲取單個元素http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java –

+1

我相信有一種方法可以做這樣的事情:'soapResponse.getSOAPBody()。getElementsByTagName()',但我一直在嘗試這樣做時會遇到一些奇怪的值。 – J33nn

回答

13

這是一些其他xml示例的整個工作示例;

public static void main(String[] args) throws IOException, SOAPException { 

    String xmlInput = " <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://litwinconsulting.com/webservices/\">\n" 
      + " <soapenv:Header/>\n" 
      + " <soapenv:Body>\n" 
      + " <web:RES>\n" 
      + "  <web:RETURNCODE>100 </web:RETURNCODE> \n" 
      + " </web:RES>\n" 
      + "  <web:GetWeather>\n" 
      + "   <!--Optional:-->\n" 
      + "   <web:City>%CITY%</web:City>\n" 
      + "  </web:GetWeather>\n" 
      + " </soapenv:Body>\n" 
      + " </soapenv:Envelope>"; 

    MessageFactory factory = MessageFactory.newInstance(); 
    SOAPMessage message = factory.createMessage(
      new MimeHeaders(), 
      new ByteArrayInputStream(xmlInput.getBytes(Charset 
        .forName("UTF-8")))); 

    SOAPBody body = message.getSOAPBody(); 

    NodeList returnList = body.getElementsByTagName("web:RES"); 

    boolean isSuccess = false; 
    for (int k = 0; k < returnList.getLength(); k++) { 
     NodeList innerResultList = returnList.item(k).getChildNodes(); 
     for (int l = 0; l < innerResultList.getLength(); l++) { 
      if (innerResultList.item(l).getNodeName() 
        .equalsIgnoreCase("web:RETURNCODE")) { 
       isSuccess = Integer.valueOf(innerResultList.item(l) 
         .getTextContent().trim()) == 100 ? true : false; 
      } 
     } 
    } 
    if (isSuccess) { 
     NodeList list = body.getElementsByTagName("web:GetWeather"); 

     for (int i = 0; i < list.getLength(); i++) { 
      NodeList innerList = list.item(i).getChildNodes(); 

      for (int j = 0; j < innerList.getLength(); j++) { 
       System.out.println(innerList.item(j).getNodeName()); 
       System.out.println(innerList.item(j).getTextContent()); 
      } 
     } 
    } 

} 

如果您需要,可以導入;

  • java.io.ByteArrayInputStream;
  • java.io.IOException;
  • java.nio.charset.Charset;
  • javax.xml.soap.MessageFactory;
  • javax.xml.soap.MimeHeaders;
  • javax.xml.soap.SOAPBody;
  • javax.xml.soap.SOAPException;
  • javax.xml.soap.SOAPMessage;
  • org.w3c.dom.NodeList;
+0

此方法有效,但它變得非常漫長,有沒有一種方法可以通過使用xPath來實現? @NoBrainer – ishanbakshi

0

在.wsdl上使用wsimport,它會爲您生成服務類。