Sending XML to webservice

-1

I need to know how to send XML to a webservice from a client. As I am quite new to this, I ran a search in some sources and found a very simple java code. I wanted to know if everything is correct:

package Envio;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.OutputKeys;
//import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class ConsumirWebServicePorRequisicaoXML {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws SOAPException, IOException {
    // TODO code application logic here
    String requestSoap;

    requestSoap = "MEU XML";

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    String url = "MEU LINK ASMX do WEBSERVICE"; //url do webservice

    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("Content-Type", "text/xml");

    MessageFactory messageFactory = MessageFactory.newInstance();

    SOAPMessage msg = messageFactory.createMessage(headers, (new ByteArrayInputStream(requestSoap.getBytes())));

    SOAPMessage soapResponse = soapConnection.call(msg, url);
    Document xmlRespostaARequisicao=soapResponse.getSOAPBody().getOwnerDocument();

    System.out.println(passarXMLParaString(xmlRespostaARequisicao,4)); //imprime na tela o xml de retorno.
}
public static String passarXMLParaString(Document xml, int espacosIdentacao){
    try {
        //set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        transfac.setAttribute("indent-number", new Integer(espacosIdentacao));
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        //create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(xml);
        trans.transform(source, result);
        String xmlString = sw.toString();
        return xmlString;
    }
    catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(0);
    }
    return null;
}
}

For obvious reasons, I omitted the XML and the ASMX link from webservice because they contained slightly more sensitive information. But I've already been able to validate the XML format and also own the webservice link.

The idea is that this application in JAVA be executed once a day or once a week and send several XML to this webservice (later I change the code to send more than one XML).

    
asked by anonymous 19.10.2016 / 14:30