I want to convert SOAPBody to String. What is the best way to do it? Should i first convert it to xml and then convert it into String or we can jsut convert it into String.
3 Answers
When starting from a SOAPMessage, the easiest way is to use the writeTo
method :
ByteArrayOutputStream stream = new ByteArrayOutputStream();
soapMessage.writeTo(stream);
String message = new String(stream.toByteArray(), "utf-8")
(Above, I assume your SAAJ implementation will use UTF-8, you'd probably want to check).
If starting from a SOAPBody, then you probably should use XML APIs, seeing SOAPBody is a org.w3.dom.Element, the easiest way would probably be using TrAX :
SOAPBody element = ... // Whatever
DOMSource source = new DOMSource(element);
StringWriter stringResult = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(source, new StreamResult(stringResult));
String message = stringResult.toString();
(Sorry I do not have my IDE right here, can not check if this compiles, but that should be pretty close).
Please note : A serialized SOAPMessage may not be raw XML : it might be a MIME structure : if the SOAPMessage actually uses SwA (SOAP With Attachment) or MTOM. However, SOAPBody is definitely pure XML.

- 9,088
- 2
- 31
- 38
-
I needed it for SOAPBody, looks like this will work. I was trying ByteArrayOutputStream with SOAPBody, but it didn't work, because of the reasons you mentioned. Thanks for the answer. – ashish2k3 Aug 05 '14 at 23:16
-
If i just do this SOAPBody element = ... //whatever String body = element.toString(); what is the drawback using toString in this case? – ashish2k3 Aug 06 '14 at 00:16
-
Calling toString on a SOAPBody object may or may not work as You expect, depending on your SAAJ implementation. however, serializing its XML content when calling toString is not part of SOAPBody's contract. Therefore, I would not trust it to run correctly everywhere / everytime – GPI Aug 06 '14 at 06:48
-
I'm wondering if calling soapMessage.writeTo(stream); helps me to bypass the DOM. I'm having a huge response as SOAPMessage and want to get the string to parse it myself using StaX, but AFAIK SAAJ uses DOM which means that the memory consumption is huge. So, will I bypass creating the DOM structure if I do soapMessage.writeTo(stream) right away when I get the response? – Ruslan Mar 28 '16 at 20:28
-
This is completely implementation dependant. Last SAAJ implementation was axis2, which uses Axiom as a parser. Axiom can defer the building of the DOM tree up to a certain point, and switch parsing style on the fly when directly acted upon (i.e. do not let axis do the job, do it yourself). Using raw SAAJ, I guess you'll be out of luck. Using parsers through an integration framework like Spring WS, you might be better of. – GPI Mar 29 '16 at 08:08
Figured this might help -
private String convertToString (SOAPBody message) throws Exception{
Document doc = message.extractContentAsDocument();
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
}
Thanks to the following post - XML Document to String?

- 1,344
- 2
- 16
- 35

- 41
- 2
You do not need to convert SOAPBody
to XML
, because it implements org.w3c.dom.Element
interface, thus this is already a valid XML
object.
You can use org.w3c.dom.ls
package to achieve your goal:
String xmlAsString = null;
Element element = what-ever-element;
DOMImplementationLS domImplementationLS = (DOMImplementationLS)element.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = domImplementationLS.createLSSerializer();
xmlAsString = serializer.writeToString(element);
You can play with serializer.getDomConfig().setParameter(....)
to configure the serializer.
-
This works only on DOM Level 3 implementations. Which, even in 2018, believe it or not, is not as mainstream as one would think (not available in some EE environments such as Weblogic 11, just out of the top of my head). But it is a nice answer nonetheless for java SE and others. – GPI Dec 13 '18 at 16:35