Java XML Document to Stream writeXmlDocumentToStream(Document document, OutputStream stream)

Here you can find the source of writeXmlDocumentToStream(Document document, OutputStream stream)

Description

Write a DOM document to an OutputStream

License

Open Source License

Declaration

public static void writeXmlDocumentToStream(Document document, OutputStream stream) throws Exception 

Method Source Code

//package com.java2s;
/**//  w  w  w.  j  a  va  2 s.c o m
 * e-Science Central
 * Copyright (C) 2008-2013 School of Computing Science, Newcastle University
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation at:
 * http://www.gnu.org/licenses/gpl-2.0.html
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
 */

import org.w3c.dom.Document;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.*;

public class Main {
    /** Write a DOM document to an OutputStream */
    public static void writeXmlDocumentToStream(Document document, OutputStream stream) throws Exception {
        Source source = new DOMSource(document);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        StreamResult result = new StreamResult(stream);
        transformer.transform(source, result);
        stream.flush();
    }
}

Related

  1. writeXml(Document doc, OutputStream outputStream)
  2. writeXML(Document document, OutputStream os)
  3. writeXML(final Document doc, final OutputStream out)
  4. writeXML(OutputStream os, Document doc)
  5. writeXml(OutputStream outputEntry, Document document)