moellerchen
moellerchen

Reputation: 51

Adding a string containing XML to a SOAPElement with Java

I have to send data from an excel file as an XML to a webservice. The data in the table looks kinda like this: see example of table here!

The first row always contains the XML-tag for the data of the column. Most of the data columns only hold strings, but some have xml. Those child-nodes are important for the Webservice to accept the data.

I'm working with SOAP, creating a new SOAPElement for each column, where every row is a new SOAP request.

SOAPElement newElement = body.addChildElement(tagForThisColumn);
newElement.addTextNode(stringValueOfCell);

This works perfectly for the string values, but the SOAPElement escapes all the "<" and ">" of the cells with xml.

I already searched for an answer and found some solutions to similar problems, but none fit mine..

Upvotes: 3

Views: 5769

Answers (1)

Roberto Molli
Roberto Molli

Reputation: 46

You should add a Node to the SoapElement. I have done something similar by adding a SAML Token to a SoapElement in the soap header:

SOAPHeader header = envelope.addHeader();

SOAPElement security = header.addChildElement("Security", "wsse",
    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

documentBuilderFactory.setNamespaceAware(true);

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
File file = new File("your-file.xml");

/* parse existing file to DOM */
Document document = documentBuilder.parse(new FileInputStream(file));

Document securityDoc = security.getOwnerDocument();
Node newNode = securityDoc.importNode(document.getFirstChild(), true);

//Add the Node
security.appendChild(newNode);

I hope it is useful.

Upvotes: 3

Related Questions