Java SOAP Message toString(SOAPMessage soapMessage)

Here you can find the source of toString(SOAPMessage soapMessage)

Description

to String

License

Apache License

Declaration

public static String toString(SOAPMessage soapMessage) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.ByteArrayOutputStream;

import java.io.OutputStream;

public class Main {
    public static String toString(SOAPMessage soapMessage) throws Exception {
        return new String(toByteArray(soapMessage));
    }//from   ww w.  j  av  a 2 s  .com

    public static String toString(Document doc) throws TransformerException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        elementToStream(doc.getDocumentElement(), baos);
        return new String(baos.toByteArray());
    }

    public static byte[] toByteArray(SOAPMessage soapMessage)
            throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        soapMessage.writeTo(bos);
        return bos.toByteArray();
    }

    public static void elementToStream(Element element, OutputStream out)
            throws TransformerException {
        DOMSource source = new DOMSource(element);
        StreamResult result = new StreamResult(out);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.transform(source, result);
    }
}

Related

  1. toByteArray(SOAPMessage soapMessage)
  2. toPrettyString(SOAPMessage response)
  3. toSOAPMessage(String msgString)
  4. toSOAPPart(String xml)
  5. toSOAPPart(String xml)
  6. writeAndRead(SOAPMessage soapMessage)