Java XML DOM Print printDom(Node dom, OutputStream os)

Here you can find the source of printDom(Node dom, OutputStream os)

Description

Print a DOM tree to an output stream or if there is an exception while doing so, print the stack trace.

License

Open Source License

Parameter

Parameter Description
dom a parameter
os a parameter

Declaration

public static void printDom(Node dom, OutputStream os) 

Method Source Code

//package com.java2s;
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.//from  w  w w .  j a  va 2s  . com
 */

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

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 {
    /**
     * Print a DOM tree to an output stream or if there is an exception while doing so, print the stack trace.
     * @param dom
     * @param os
     */
    public static void printDom(Node dom, OutputStream os) {
        Transformer trans;
        PrintWriter w = new PrintWriter(os);
        try {
            TransformerFactory fact = TransformerFactory.newInstance();
            trans = fact.newTransformer();
            trans.transform(new DOMSource(dom), new StreamResult(
                    new OutputStreamWriter(os)));
        } catch (TransformerException e) {
            w.println("An error ocurred while transforming the given DOM:");
            e.printStackTrace(w);
        }
    }
}

Related

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