Java XML Document to Stream writeDocument(OutputStream os, Node node)

Here you can find the source of writeDocument(OutputStream os, Node node)

Description

Writes the given node to the given output stream.

License

Open Source License

Parameter

Parameter Description
os an output stream
node a DOM node

Declaration

public static void writeDocument(OutputStream os, Node node) 

Method Source Code

//package com.java2s;

import java.io.OutputStream;

import org.w3c.dom.DOMImplementation;

import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;

import org.w3c.dom.ls.LSOutput;

import org.w3c.dom.ls.LSSerializer;

public class Main {
    private static DOMImplementation impl;
    private static DOMImplementationRegistry registry;

    /**/*from   ww  w  .j ava2  s .c  o m*/
     * Writes the given node to the given output stream.
     * 
     * @param os
     *            an output stream
     * @param node
     *            a DOM node
     */
    public static void writeDocument(OutputStream os, Node node) {
        getImplementation();
        DOMImplementationLS implLS = (DOMImplementationLS) impl;

        // serialize to XML
        LSOutput output = implLS.createLSOutput();
        output.setByteStream(os);

        // serialize the document, close the stream
        LSSerializer serializer = implLS.createLSSerializer();
        serializer.getDomConfig().setParameter("format-pretty-print", true);
        serializer.write(node, output);
    }

    /**
     * Creates a new instance of the DOM registry and get an implementation of DOM 3 with Load Save
     * objects.
     */
    private static void getImplementation() {
        try {
            if (registry == null) {
                registry = DOMImplementationRegistry.newInstance();
            }

            if (impl == null) {
                impl = registry.getDOMImplementation("Core 3.0 XML 3.0 LS");
                if (impl == null) {
                    throw new RuntimeException("no DOM 3 implementation found");
                }
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("DOM error", e);
        } catch (InstantiationException e) {
            throw new RuntimeException("DOM error", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("DOM error", e);
        }
    }
}

Related

  1. writeDocument(Document document, OutputStream documentOutputStream)
  2. writeDocument(Document document, OutputStream os)
  3. writeDocument(Document document, OutputStream outputStream)
  4. writeDocument(OutputStream iStream, Element iElement)
  5. writeDocument(OutputStream os, Document document)
  6. writeDocument(OutputStream stream, Document document)
  7. writeDOMDocumentToStream(Document doc, OutputStream stream)
  8. writeOutDocument(OutputStream os, Document doc)
  9. writeTransformedXml(Document doc, OutputStream output, InputStream style)