从java中的soap响应中获取数据

时间:2015-01-14 14:06:47

标签: soap

我的服务类是 公共类WebService {

public String getName(){

    return "bader";

}

我通过

调用此服务
try {           
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
        String url = "http://localhost:8080/mywebservice/services/WebService?wsdl";

        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
        System.out.println("here is "+soapResponse.getSOAPBody().getChildElements());
        SOAPBody responseBody = soapResponse.getSOAPBody();
        QName bodyName1 = new QName("http://sample.dass","getNameResponse");

        java.util.Iterator iterator = responseBody.getChildElements(bodyName1);

        while (iterator.hasNext()) {
          SOAPBodyElement responseElement = (SOAPBodyElement)iterator.next();
          String val = responseElement.getValue();
          System.out.println("values are "+val);
        }
        printSOAPResponse(soapResponse);
        //soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }
    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        System.out.println("");
        soapMessage.saveChanges();

        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();
        return soapMessage;
    }

    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);
    }
肥皂反应是 响应SOAP消息= bader

我想在变量

中获得返回值“bader”

0 个答案:

没有答案