Java XML Document to Writer toWriter(Document doc, Writer writer)

Here you can find the source of toWriter(Document doc, Writer writer)

Description

to Writer

License

LGPL

Declaration

public static void toWriter(Document doc, Writer writer) 

Method Source Code

//package com.java2s;
/*/*from   w w  w . j a va  2 s  .c om*/
 * JFox - The most lightweight Java EE Application Server!
 * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
 *
 * JFox is licenced and re-distributable under GNU LGPL.
 */

import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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 {
    private final static TransformerFactory tf = TransformerFactory.newInstance();

    public static void toWriter(Document doc, Writer writer) {
        if (doc == null || writer == null) {
            return;
        }
        try {
            Transformer tran = tf.newTransformer();
            tran.setOutputProperty(OutputKeys.INDENT, "yes");
            Source src = new DOMSource(doc);
            Result res = new StreamResult(writer);
            tran.transform(src, res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. toXML(Document doc, Writer w, boolean indent)
  2. write(Writer out, Document doc)
  3. writeDocument(Document doc, Writer out)
  4. writeDocument(Document doc, Writer w)