简体   繁体   中英

How can I obtain an XML file with web services using SOAP in a Java Web application?

Hi I've done some research, but I still can't solve my problem. I have to obtain a value from my bank and print the value. They provide a web services site with the following code (already replaced the variables w/ the correct values)

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

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>

Here's what I've done so far

    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 file:

<?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>

How can I parse the response and print the response value? Thank you for any help!

you can use this way also

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

then you can loop on the returnlist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM