Search code examples
androidxmlweb-servicesksoap2

send xml as message in a xml


I need to send 3 things from my app to a web service: username + password and a message which contains more XML.

request.addProperty("username", username);
request.addProperty("password", password);
request.addProperty("message", UitVoer); // uitvoer is a String that contains XML

I use the ksoap2 librabry which always worked great for android <-> web service, but atm I'm not sure if it still works for what I need.

When I check my envelope.bodyOut, all tags and < > signs look like they should. But then I get an error message from the web service with the XML I sent, and it shows the HTML code for those signs.

Does ksoap2 replace the signs and should I use another way to send my stuff to the web service, or is there something wrong on the web service side? (I have no control over the web service so I have no clue what it does on that side.)


Solution

  • This is how you build an xml request; hope this helps:)

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();  
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();  
    Document document = documentBuilder.newDocument();  
    Element rootElement = document.createElement("map");  
    document.appendChild(rootElement);  
    
    Element em = document.createElement("string");  
    em.setAttribute("name", "FirstName");  
    em.appendChild(document.createTextNode("Rita"));  
    rootElement.appendChild(em);  
    
    em = document.createElement("string");  
    em.setAttribute("name", "LastName");  
    em.appendChild(document.createTextNode("Roy"));  
    rootElement.appendChild(em);  
    
    
    TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();
                Properties outFormat = new Properties();
                outFormat.setProperty(OutputKeys.INDENT, "yes");
                outFormat.setProperty(OutputKeys.METHOD, "xml");
                outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                outFormat.setProperty(OutputKeys.VERSION, "1.0");
                outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
                transformer.setOutputProperties(outFormat);
                DOMSource domSource = new DOMSource(document.getDocumentElement());
                OutputStream output = new ByteArrayOutputStream();
                StreamResult result = new StreamResult(output);
                transformer.transform(domSource, result);
                strXMLInput = output.toString();