Hi,
I have written a function to pretty print a javax.xml.soap.SOAPMessage to a string:
public static String soapMessageToString (SOAPMessage msg) {
try {
Writer out = new StringWriter();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(msg.getSOAPPart().getContent(), new StreamResult(out));
return out.toString();
} catch (Exception e) {
}
return "";
}
}
The issue is that this does not properly encode the entire soap body.
On the wire, I see:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetSpeechResponse xmlns="http://xmlme.com/WebServices"><GetSpeechResult>&lt;SPEECH&gt;&lt;PLAY&gt;MACBETH&lt;/PLAY&gt;&lt;SPEAKER&gt;LENNOX&lt;/SPEAKER&gt;Or so much as it needs, To dew the sovereign flower and drown the weeds. Make we our march towards Birnam.</SPEECH></GetSpeechResult></GetSpeechResponse></soap:Body></soap:Envelope>
And my pretty printer yields:
Sep 19, 2009 5:44:32 PM com.xmlme.webservices.LoggingHandler handleMessage
FINE: Response:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header/>
<soap:Body>
<GetSpeechResponse xmlns="http://xmlme.com/WebServices">
<GetSpeechResult><SPEECH><PLAY>MACBETH</PLAY><SPEAKER>LENNOX</SPEAKER>Or so much as it needs, To dew the sovereign flower and drown the weeds. Make we our march towards Birnam.</SPEECH></GetSpeechResult>
</GetSpeechResponse>
</soap:Body>
</soap:Envelope>
Notice that I see the < and the > in the response. I have tried setting the Transformers output property:
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
and I have tried setting the SOAPMessage property:
msg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
..but no dice.
Am I missing something obvious?