Java XML DOM Print printDOMTree(Node node, PrintWriter out, String docType, String copyright)

Here you can find the source of printDOMTree(Node node, PrintWriter out, String docType, String copyright)

Description

Debugging method for printing out the DOM Tree Prints the specified node, recursively.

License

Open Source License

Parameter

Parameter Description
node a parameter
out a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void printDOMTree(Node node, PrintWriter out,
        String docType, String copyright) throws IOException 

Method Source Code

//package com.java2s;

import java.io.IOException;
import java.io.PrintWriter;

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.  ja v a2s  .co  m*/
     * Debugging method for printing out the DOM Tree
     * Prints the specified node, recursively.
     *
     * @param node
     * @param out
     * @throws IOException
     */
    public static void printDOMTree(Node node, PrintWriter out,
            String docType, String copyright) throws IOException {
        try {
            Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(
                    "{http://xml.apache.org/xslt}indent-amount", "4");

            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    "yes");
            out.print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
            if (copyright != null) {
                out.print(copyright);
            }
            if (docType != null) {
                out.print(docType);
            }

            // transformer.setParameter(entityName, entityRef );
            // transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, XLIFF_PUBLIC_NAME);
            transformer.transform(new DOMSource(node),
                    new StreamResult(out));
        } catch (TransformerException te) {
            throw new IOException(te.getMessage());
        }

        // out.close();
    }
}

Related

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