gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:41:56 30 4
gpt4 key购买 nike

我正在尝试与 SOAP 服务交互。我能够获取 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());
}

有人可以建议我一个关于如何将我收到的响应保存到本地文件的片段吗?

目前,在上面的代码中,响应正在控制台中显示。

最佳答案

您可以使用 SOAPMessage 接口(interface)的 writeTo 方法来执行相同的操作,例如:

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

维诺德。

关于java - 使用JAVA解析SOAP响应并提取特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388429/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com