Java XML Document to Stream writeDocument(Document doc, OutputStream os)

Here you can find the source of writeDocument(Document doc, OutputStream os)

Description

write Document

License

Open Source License

Declaration

public static void writeDocument(Document doc, OutputStream os) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

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

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

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 {
    public static void writeDocument(Document doc, OutputStream os) throws IOException {
        try {/*w  w w  .j  a  v  a2s .  co  m*/
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");

            DOMSource source = new DOMSource(doc);
            CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder();
            StreamResult result = new StreamResult(new OutputStreamWriter(os, utf8Encoder));
            transformer.transform(source, result);
        } catch (TransformerException e) {
            System.err.println(e.getMessage());
        }
    }
}

Related

  1. dumpToStream(Document doc, OutputStream out)
  2. toXml(Document doc, OutputStream out)
  3. write(Document doc, OutputStream out, String enc)
  4. write(Document document, OutputStream byteStream)
  5. writeDocument(Document doc, OutputStream os)
  6. writeDocument(Document doc, Transformer transformer, OutputStream out)
  7. writeDocument(Document document, OutputStream documentOutputStream)
  8. writeDocument(Document document, OutputStream os)
  9. writeDocument(Document document, OutputStream outputStream)