[英]How can I obtain an XML file with web services using SOAP in a Java Web application?
嗨,我已经做了一些研究,但仍然无法解决我的问题。 我必须从我的银行获得价值并打印出来。 他们为Web服务站点提供以下代码(已经用正确的值替换了变量)
以下是SOAP 1.2请求和响应的示例。 显示的占位符需要替换为实际值。
POST /indicadoreseconomicos/WebServices/wsIndicadoresEconomicos.asmx HTTP/1.1
Host: indicadoreseconomicos.bccr.fi.cr
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ObtenerIndicadoresEconomicosXML xmlns="http://ws.sdde.bccr.fi.cr">
<tcIndicador>317</tcIndicador>
<tcFechaInicio>14/05/2015</tcFechaInicio>
<tcFechaFinal>15/05/2015</tcFechaFinal>
<tcNombre>Bove</tcNombre>
<tnSubNiveles>S</tnSubNiveles>
</ObtenerIndicadoresEconomicosXML>
</soap12:Body>
</soap12:Envelope>
到目前为止,这是我所做的
public class ParseTest {
public static void parse() throws IOException, TransformerConfigurationException, TransformerException {
String fileRequestInput = "src/parsetest/XMLBanco.xml";
File xmlFileRequest = new File(fileRequestInput);
File file = new File(fileRequestInput);
FileInputStream fstreamRequest = new FileInputStream(xmlFileRequest);
DocumentBuilderFactory docFactory = null;
DocumentBuilder docBuilder = null;
Document document = null;
try {
docFactory = DocumentBuilderFactory.newInstance();
docBuilder = docFactory.newDocumentBuilder();
document = docBuilder.parse(fstreamRequest);
} catch (Exception e) {
System.out.println("FAILED");
}
System.out.println("Request xml generated Parsed succesffully ");
System.out.println(document);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(document);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
}
}
XML档案:
<?xml version="1.0" encoding="UTF-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<ObtenerIndicadoresEconomicosXML xmlns="http://ws.sdde.bccr.fi.cr">
<tcIndicador>317</tcIndicador>
<tcFechaInicio>14/05/2015</tcFechaInicio>
<tcFechaFinal>15/05/2015</tcFechaFinal>
<tcNombre>Bove</tcNombre>
<tnSubNiveles>S</tnSubNiveles>
</ObtenerIndicadoresEconomicosXML>
</soap12:Body>
</soap12:Envelope>
如何解析响应并打印响应值? 感谢您的任何帮助!
你也可以用这种方式
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage(
new MimeHeaders(),
new ByteArrayInputStream(xmlInput.getBytes(Charset
.forName("UTF-8"))));
SOAPBody body = message.getSOAPBody();
NodeList returnList = body.getElementsByTagName("ObtenerIndicadoresEconomicosXML");
那么你就可以循环返回列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.