简体   繁体   English

解析SOAP响应并使用JAVA提取特定节点

[英]Parse SOAP response and extract specific node using JAVA

I'm trying to interact with a SOAP service. 我正在尝试与SOAP服务进行交互。 I'm able to get the SOAP response and by using Source sourceContent = soapResponse.getSOAPPart().getContent(); 我可以通过使用Source sourceContent = soapResponse.getSOAPPart().getContent();获得SOAP响应Source sourceContent = soapResponse.getSOAPPart().getContent(); and transformer.transform(sourceContent, result); transformer.transform(sourceContent, result); , I'm able to see what the output/response is and displaying it in the console. ,我可以看到输出/响应是什么并将其显示在控制台中。

But, I need to extract sessionID from the response and send that sessionID in a different SOAP request. 但是,我需要从响应中提取sessionID并在另一个SOAP请求中发送该sessionID

Please suggest me the extraction method, building a new SOAP request 请建议我提取方法,建立一个新的SOAP请求

Parsing is what I need to do!! 解析是我需要做的!

Below is the code for sending the request to the SOAP service: 以下是用于将请求发送到SOAP服务的代码:

public static void main(String args[]) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "https://WWW.DUMMYURL.COM";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);


        // Process the SOAP Response
        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();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        String serverURN = "urn:DUMMYURL.COM";
        String serverNS0 = "http://WWW.DUMMYURL.COM";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("urn", serverURN);
        envelope.addNamespaceDeclaration("ns0", serverNS0);

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();     
        SOAPElement soapBodyElem1 = soapBody.addChildElement("login","urn");
        SOAPElement soapBodyElem2 = soapBodyElem1.addChildElement("username","urn");
        @SuppressWarnings("unused")
        SOAPElement soapBodyElem3 = soapBodyElem2.addTextNode("USERNAME");
        SOAPElement soapBodyElem4 = soapBodyElem1.addChildElement("password","urn");
        @SuppressWarnings("unused")
        SOAPElement soapBodyElem5 = soapBodyElem4.addTextNode("PASSWORD");
        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", "https://WWW.DUMMYURL.COM"  + "login");
        soapMessage.saveChanges();

        //Print the request message
        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();
        String source = sourceContent.toString();
        System.out.print("\nResponse SOAP Message = ");

        // Format it
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
        System.out.println(transformer.toString());
}

Can someone please suggest me a snippet on how should I save the response I'm getting to a file locally? 有人可以请我向我推荐一个片段, 该如何保存我在本地保存到文件的响应?

Currently in the above code the response is getting displayed in the console. 当前,在上面的代码中, response正在控制台中显示。

You can use the writeTo method of the SOAPMessage interface to do the same, for example: 您可以使用SOAPMessage接口的writeTo方法执行此操作,例如:

FileOutputStream out = new FileOutputStream("somefile");
soapResponse.writeTo(out);

Vinod. 维诺德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM