【问题标题】:How can I obtain an XML file with web services using SOAP in a Java Web application?如何在 Java Web 应用程序中使用 SOAP 获取带有 Web 服务的 XML 文件?
【发布时间】:2015-05-17 05:55:12
【问题描述】:

您好,我进行了一些研究,但仍然无法解决我的问题。我必须从我的bank 获取一个值并打印该值。他们提供了一个带有以下代码的 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>

如何解析响应并打印响应值? 感谢您的帮助!

【问题讨论】:

  • 看看他们是否有WSDL文件,然后你可以从中生成java客户端代码,然后你可以使用它的方法来获取你想要的值。

标签: java html xml web-services soap


【解决方案1】:

你也可以这样用

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");

然后你可以循环返回列表

【讨论】:

    猜你喜欢
    • 2019-05-23
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2012-01-29
    相关资源
    最近更新 更多