Java XML Format prettyPrintXml(SOAPMessage message)

Here you can find the source of prettyPrintXml(SOAPMessage message)

Description

pretty Print Xml

License

Open Source License

Declaration

public static String prettyPrintXml(SOAPMessage message)
            throws SOAPException, IOException,
            TransformerFactoryConfigurationError, TransformerException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    public static String prettyPrintXml(SOAPMessage message)
            throws SOAPException, IOException,
            TransformerFactoryConfigurationError, TransformerException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        message.writeTo(out);//from  w  ww  .  j a  va 2s. c  om

        return prettyPrintXml(out.toString("UTF-8"));
    }

    public static String prettyPrintXml(String unformattedXml)
            throws TransformerFactoryConfigurationError,
            UnsupportedEncodingException, TransformerException {

        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(new StreamSource(new ByteArrayInputStream(
                unformattedXml.getBytes("UTF-8"))), result);
        return result.getWriter().toString();
    }
}

Related

  1. prettyFormatXml(final InputStream xml, final OutputStream os, final int indent)
  2. prettyFormatXmlText(String text)
  3. prettyPrint(final Source source)
  4. prettyPrint(String header, String xml)
  5. prettyPrintToString(String xml)
  6. prettyPrintXml(String input)
  7. prettyPrintXml(String input)
  8. prettyPrintXML(String xml)
  9. prettyXml(final String xml)