Dienstag, 16. Juli 2013

Pretty print a SOAP Message

Here's a code snip for pretty printing a SOAP message 

import java.io.ByteArrayOutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;


    private static final Logger LOG = Logger.getLogger(PrettyPrint.class);
 

    /**
     * Pretty print the XML SOAP message
     *
     * @param source the source payload of a SOAP response or request
     * @return the pretty format of the SOAP message
     */

    private String getPrettyPrintSoapSource(Source source) {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer();
            t.setOutputProperty(OutputKeys.INDENT, "yes");
            t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(bos);
            t.transform(source, result);
            return bos.toString();
        } catch (Exception e) {
            LOG.error("Error while pretty printing SOAP source", e);
            return "Error while pretty printing SOAP source";
        }
    }

Keine Kommentare:

Kommentar veröffentlichen