Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert SOAPBody to String

Tags:

java

string

soap

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.

like image 214
ashish2k3 Avatar asked Aug 05 '14 22:08

ashish2k3


People also ask

How to convert SOAP response to String?

SOAPBody element = ... // Whatever DOMSource source = new DOMSource(element); StringWriter stringResult = new StringWriter(); TransformerFactory. newInstance(). newTransformer(). transform(source, new StreamResult(stringResult)); String message = stringResult.

How to convert SOAP request from String in Java?

Convert the String into an input stream, then read it into the SOAP message factory. InputStream is = new ByteArrayInputStream(send. getBytes()); SOAPMessage request = MessageFactory. newInstance().

How do you make a SOAP envelope in Java?

SOAPEnvelope envelope = soapPart. getEnvelope(); You can now use the getHeader and getBody methods of envelope to retrieve its empty SOAPHeader and SOAPBody objects. SOAPHeader header = envelope.


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

like image 198
GPI Avatar answered Oct 06 '22 01:10

GPI


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?

like image 36
Atanu Mukherjee Avatar answered Oct 05 '22 23:10

Atanu Mukherjee