Java XML Document to String toStream(Document document, OutputStream stream)

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

Description

Saves an XML Document into a stream.

License

Open Source License

Parameter

Parameter Description
document The XML document to persist.
stream The stream to write into.

Exception

Parameter Description
TransformerException an exception
TransformerConfigurationException an exception

Declaration

public static void toStream(Document document, OutputStream stream)
        throws TransformerException, TransformerConfigurationException 

Method Source Code

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

import java.io.OutputStream;

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

public class Main {
    /**//  w w w  .ja  v a  2s . com
     * Saves an XML Document into a stream.
     * 
     * @param document
     *            The XML document to persist.
     * @param stream
     *            The stream to write into.
     * @throws TransformerException
     * @throws TransformerConfigurationException
     */
    public static void toStream(Document document, OutputStream stream)
            throws TransformerException, TransformerConfigurationException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        Source input = new DOMSource(document);
        Result output = new StreamResult(stream);
        transformer.transform(input, output);
    }
}

Related

  1. getXml(Document doc)
  2. getXML(Document doc)
  3. getXML(Document pDocument)
  4. toByteArray(final Document document)
  5. toByteArray(final Document document)
  6. toString(Document d)
  7. toString(Document doc)
  8. toString(Document doc)
  9. toString(Document doc)