Java XML DOM Print printDOM(Node root, OutputStream os)

Here you can find the source of printDOM(Node root, OutputStream os)

Description

Prints DOM to output stream.

License

Apache License

Parameter

Parameter Description
root root node of the document or document itself
os stream to output

Exception

Parameter Description
TransformerException an exception

Declaration

public static void printDOM(Node root, OutputStream os) throws TransformerException 

Method Source Code

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

import org.w3c.dom.Node;

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 TransformerFactory trFactory = null;

    /**/* ww  w  .j  a  v  a 2s . c o m*/
     * Prints DOM to output stream. Uses XSLT identity transform for conversion of DOM to string.
     *
     * @param root root node of the document or document itself
     * @param os   stream to output
     * @throws TransformerException
     */
    public static void printDOM(Node root, OutputStream os) throws TransformerException {
        identityTransform(root, new StreamResult(os));
    }

    private static void identityTransform(Node root, Result res) throws TransformerException {
        createTransformerFactory();
        Transformer tr = trFactory.newTransformer();
        tr.setOutputProperty(OutputKeys.METHOD, "xml");
        tr.setOutputProperty(OutputKeys.INDENT, "yes");
        tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tr.transform(new DOMSource(root), res);
    }

    private static void createTransformerFactory() {
        if (trFactory == null)
            trFactory = TransformerFactory.newInstance();
    }
}

Related

  1. prettyPrintDOM(Node node)
  2. prettyPrintDOM(Node node, OutputStream stream)
  3. prettyPrintDOMAsHTML(Node node, OutputStream stream)
  4. printDom(Node dom, OutputStream os)
  5. printDOM(Node node, OutputStream out, String encoding)
  6. printDOMTree(Node node, PrintWriter out, String docType, String copyright)
  7. printTreeDOM(final Element samlToken, final boolean isIndent)