繁体   English   中英

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

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

我正在尝试与SOAP服务进行交互。 我可以通过使用Source sourceContent = soapResponse.getSOAPPart().getContent();获得SOAP响应Source sourceContent = soapResponse.getSOAPPart().getContent(); transformer.transform(sourceContent, result); ,我可以看到输出/响应是什么并将其显示在控制台中。

但是,我需要从响应中提取sessionID并在另一个SOAP请求中发送该sessionID

请建议我提取方法,建立一个新的SOAP请求

解析是我需要做的!

以下是用于将请求发送到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());
}

有人可以请我向我推荐一个片段, 该如何保存我在本地保存到文件的响应?

当前,在上面的代码中, response正在控制台中显示。

您可以使用SOAPMessage接口的writeTo方法执行此操作,例如:

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

维诺德

暂无
暂无

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

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