views:

426

answers:

1

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"&gt;&lt;soap:Body&gt;&lt;GetSpeechResponse xmlns="http://xmlme.com/WebServices"&gt;&lt;GetSpeechResult&gt;&amp;lt;SPEECH&amp;gt;&amp;lt;PLAY&amp;gt;MACBETH&amp;lt;/PLAY&amp;gt;&amp;lt;SPEAKER&amp;gt;LENNOX&amp;lt;/SPEAKER&amp;gt;Or so much as it needs, To dew the sovereign flower and drown the weeds. Make we our march towards Birnam.&lt;/SPEECH&gt;</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"&gt;
    <soap:Header/>
    <soap:Body>
        <GetSpeechResponse xmlns="http://xmlme.com/WebServices"&gt;
            <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.&lt;/SPEECH&gt;</GetSpeechResult>
        </GetSpeechResponse>
    </soap:Body>
</soap:Envelope>

Notice that I see the &lt and the &gt 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?

A: 

I experience the same problem. I don't think it's related to the character encoding; seems more like a Transformer doing you a "favor".

Have you found a solution?

dasp