Java XML Document Format printDocument(Document doc, OutputStream out)

Here you can find the source of printDocument(Document doc, OutputStream out)

Description

print Document

License

Open Source License

Parameter

Parameter Description
doc a parameter
out a parameter

Exception

Parameter Description
IOException an exception
TransformerException an exception

Declaration

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version./*from   w  w w.j  a  va 2s. co m*/
 *   
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *    
 * Contributors:
 *               Kamesh Sampath - initial implementation
 *******************************************************************************/

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

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.Document;

public class Main {
    /**
     * @param doc
     * @param out
     * @throws IOException
     * @throws TransformerException
     */
    public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$

        transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); //$NON-NLS-1$
    }
}

Related

  1. printDoc(Document doc)
  2. printDoc(Document doc, PrintStream pstrm)
  3. printDoctype(Document doc, PrintStream pstrm)
  4. printDocument(Document d)
  5. printDocument(Document doc, boolean prettyPrint)
  6. printDocument(Document doc, Writer ps)
  7. printDocument(Document document)
  8. printDocument(Document document)
  9. printDocument(Document document)