Java XML Document to File writeXmlToStream(Document doc, OutputStream stream)

Here you can find the source of writeXmlToStream(Document doc, OutputStream stream)

Description

write Xml To Stream

License

Open Source License

Declaration

public static void writeXmlToStream(Document doc, OutputStream stream) throws Exception 

Method Source Code

//package com.java2s;

import java.io.OutputStream;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 writeXmlToStream(Document doc, OutputStream stream) throws Exception {
        Result result = new StreamResult(stream);
        transform(doc, result);//w  w w  .  j  a va2  s.  co m
    }

    private static void transform(Document doc, Result result) throws Exception {
        try {
            // Prepare the DOM document for writing
            Source source = new DOMSource(doc);

            // Write the DOM document to the file
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
            xformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
            throw e;
        } catch (TransformerException e) {
            e.printStackTrace();
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }
}

Related

  1. writeXmlFile(Document doc, String filename)
  2. writeXMLFile(Document document, Writer writer)
  3. writeXmlFile(File file, Document document)
  4. writeXmlFile(String fileName, Document document)
  5. WriteXMLFile2(Document doc, String strFilePath)
  6. writeXMLtoStream(Object osw, Document doc)