Java XML Document to Stream dom2InputStream(Document doc)

Here you can find the source of dom2InputStream(Document doc)

Description

dom Input Stream

License

Open Source License

Declaration

public static InputStream dom2InputStream(Document doc) 

Method Source Code


//package com.java2s;
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    public static InputStream dom2InputStream(Document doc) {

        InputStream is = null;//from w w w  . j a v  a2 s  .  co  m
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            Source xmlSource = new DOMSource(doc);
            Result outputTarget = new StreamResult(outputStream);
            TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
            is = new ByteArrayInputStream(outputStream.toByteArray());
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return is;

    }
}

Related

  1. DocumentToInputStream(Document edoc)
  2. documentToStream(Document document, OutputStream outputStream, String encoding)
  3. dumpDoc(Document domTree, PrintStream out)
  4. dumpToStream(Document doc, OutputStream out)
  5. toXml(Document doc, OutputStream out)
  6. write(Document doc, OutputStream out, String enc)