Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SOAP Body with Two Elements

I am trying to create a soap message with the following structure:

<SOAP:Envelope>
    <SOAP:Header/>
    <SOAP:Body>
        <Item1/>
        <Item2/>
    </SOAP:Body>
</SOAP:Envelope>

I am using a WebServiceMessageCallback to get the above format, but when I try and transform the body with the new source, where source has the above format:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, soapBody.getPayloadResult());

I get the following error:

[Fatal Error] :1:157: The markup in the document following the root element must be well-formed.
ERROR:  'The markup in the document following the root element must be well-formed.'
    Exception in thread "main" org.springframework.ws.client.WebServiceTransformerException:Transformation error: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 157; The markup in the document following the root element must be well-formed.; nested exception is javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 157; The markup in the document following the root element must be well-formed.

My understanding is that this error is telling me that the body is not well-formed XML because it doesn't have a single root element. However, that is what I need as the service I am sending the message to expects the above structure.

Is there some way around this? Thanks for any help.

EDIT: I should mention that I have seen Spring Update SOAP Header with Two Elements, which is what I am trying to do but in the header instead. I have tried his method of using a dummy root, but I am not sure how to remove the dummy root after the transformation as he mentions. I don't have enough rep to comment on his answer to find out...

like image 885
Esc Tab Avatar asked Jul 05 '26 17:07

Esc Tab


1 Answers

The element <SOAP:Body> contains the message you want to exchange. Putting in two elements in a <SOAP:Body> is therefore not possible. You have to wrap a root XML element around your two items.

like image 138
rowing-ghoul Avatar answered Jul 07 '26 06:07

rowing-ghoul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!