Java XML Format printXML(Node node)

Here you can find the source of printXML(Node node)

Description

Transforms an XML to a String

License

Apache License

Parameter

Parameter Description
node XML node

Return

String represenation of XML

Declaration

public static String printXML(Node node) 

Method Source Code

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

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;

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 org.w3c.dom.Node;

public class Main {
    /**/*from  www. j  a v  a2s  .  co m*/
     * Transforms an XML to a String
     * @param node XML node
     * @return String represenation of XML
     */
    public static String printXML(Node node) {
        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer serializer;
        try {
            serializer = tfactory.newTransformer();

            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

            StringWriter output = new StringWriter();
            serializer.transform(new DOMSource(node), new StreamResult(output));
            return output.toString();
        } catch (TransformerException e) {

            throw new RuntimeException(e);
        }
    }
}

Related

  1. printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration)
  2. printNodeSubtreeXMLString(final Node node)
  3. printNodeToConsole(Node n, ByteArrayOutputStream byteArrayOS)
  4. printReply(SOAPMessage reply)
  5. printStream(InputStream is)
  6. xmlFormat(String xml)
  7. xmlFormat(String xml)