Java XML Document Print printDocument(final Document doc)

Here you can find the source of printDocument(final Document doc)

Description

print Document

License

Open Source License

Declaration

public static void printDocument(final Document doc) 

Method Source Code

//package com.java2s;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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.Document;

public class Main {
    public static void printDocument(final Document doc) {
        try {/*from  ww  w  . ja v a 2s  .c  o  m*/
            final TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer;
            transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    "no");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(
                    "{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.transform(new DOMSource(doc), new StreamResult(
                    new OutputStreamWriter(System.out, "UTF-8")));
        } catch (final TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (final TransformerException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. printDocument(File file)
  2. printDocument(Node _docOrNode, OutputStream _outStream)
  3. printDocument(Node doc)
  4. printDocumentToStdout(final Document document)
  5. printDocumentTree(Node el)