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