2016-07-15 4 views
0

SOAPサービスと対話しようとしています。 SOAP応答を取得できました。Source sourceContent = soapResponse.getSOAPPart().getContent();transformer.transform(sourceContent, result);を使用すると、出力/応答が何であるかを確認してコンソールに表示できます。SOAP応答を解析し、JAVAを使用して特定のノードを抽出します

しかし、私は応答から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がコンソールに表示されています。

答えて

1

は、次の例のように、同じことを行うためにSOAPMessageインタフェースのwriteTo方法を使用することができます。

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

のVinodを。

関連する問題