Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a string containing XML to a SOAPElement with Java

Tags:

java

soap

xml

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

like image 582
moellerchen Avatar asked Sep 08 '26 06:09

moellerchen


1 Answers

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.

like image 148
Roberto Molli Avatar answered Sep 10 '26 19:09

Roberto Molli