2013-12-09 25 views
10

Tôi đang cố gắng nhận các giá trị cụ thể từ phản hồi tôi nhận được từ dịch vụ web. Thật không may tôi không biết làm thế nào để làm điều đó. Tôi đã sử dụng mã được tìm thấy trên stackoverflow để tạo yêu cầu xà phòng và viết ra nội dung trả lời vào stdout:Yêu cầu xà phòng Java - đọc phản hồi xà phòng

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); 
} 

Tất cả đều hoạt động tốt nhưng tôi không cần nội dung đáp ứng toàn bộ:

<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> 

Tôi chỉ cần giá trị mã và mô tả từ phản hồi này. Tôi có thể làm cái này như thế nào?

+0

hãy xem XPATH nếu bạn đang cố gắng lấy một phần tử duy nhất từ ​​thông báo xà phòng http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java –

+1

Tôi tin rằng có một cách để làm một cái gì đó như: 'soapResponse.getSOAPBody(). GetElementsByTagName()' nhưng tôi tiếp tục nhận được một số giá trị lạ trong khi cố gắng làm như thế này. – J33nn

Trả lời

13

Đây là toàn bộ ví dụ làm việc cho một số ví dụ xml khác;

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()); 
      } 
     } 
    } 

} 

và nhập nếu bạn cần;

  • 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

Phương pháp này hoạt động, nhưng nó nhận được rất dài, có cách nào tôi có thể làm điều này bằng cách sử dụng xPath? @Không có trí tuệ – ishanbakshi

0

Sử dụng wsimport trên .wsdl và nó sẽ tạo các lớp dịch vụ cho bạn.

Các vấn đề liên quan