[英]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.