Java XML Format printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration)

Here you can find the source of printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration)

Description

print Node

License

Open Source License

Declaration

public static void printNode(OutputStream out, Node node,
            boolean prettyPrint, boolean includeXmlDeclaration) 

Method Source Code

//package com.java2s;
import org.w3c.dom.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.OutputStream;

public class Main {
    private static final TransformerFactory tFactory = TransformerFactory
            .newInstance();

    public static void printNode(OutputStream out, Node node,
            boolean prettyPrint, boolean includeXmlDeclaration) {
        try {/*from   ww w.j  a  v a 2  s .  c  om*/
            Transformer serializer = tFactory.newTransformer();
            if (prettyPrint) {
                //Setup indenting to "pretty print"
                serializer.setOutputProperty(OutputKeys.INDENT, "yes");
                serializer.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount", "2");
            }
            if (!includeXmlDeclaration) {
                serializer.setOutputProperty(
                        OutputKeys.OMIT_XML_DECLARATION, "yes");
            }

            DOMSource xmlSource = new DOMSource(node);
            StreamResult outputTarget = new StreamResult(out);
            serializer.transform(xmlSource, outputTarget);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. print(SOAPMessage msg, OutputStream out)
  2. printAttributeValue(Element node, String prefix)
  3. printNode(Node n)
  4. printNode(Node node)
  5. printNode(Node node, String fn)
  6. printNodeSubtreeXMLString(final Node node)
  7. printNodeToConsole(Node n, ByteArrayOutputStream byteArrayOS)
  8. printReply(SOAPMessage reply)
  9. printStream(InputStream is)