Java Utililty Methods XML Document to Stream

List of utility methods to do XML Document to Stream

Description

The list of methods to do XML Document to Stream are organized into topic(s).

Method

InputStreamDocumentToInputStream(Document edoc)
Converts an org.w3c.dom.Document element to an java.io.InputStream.
final org.w3c.dom.Document doc = edoc;
final PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
pis.connect(pos);
(new Thread(new Runnable() {
    public void run() {
        try {
            TransformerFactory tFactory = TransformerFactory.newInstance();
...
voiddocumentToStream(Document document, OutputStream outputStream, String encoding)
Writes the document to the given output stream with the given encoding.
Source source = new DOMSource(document);
Result result = new StreamResult(outputStream);
Transformer transformer;
try {
    transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    if (document.getXmlEncoding() == null) {
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
...
InputStreamdom2InputStream(Document doc)
dom Input Stream
InputStream is = null;
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) {
...
voiddumpDoc(Document domTree, PrintStream out)
dump Doc
TransformerFactory transformerFact = TransformerFactory.newInstance();
transformerFact.setAttribute("indent-number", 4);
Transformer transformer = transformerFact.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(domTree);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
...
voiddumpToStream(Document doc, OutputStream out)
dump To Stream
PrintWriter w = new PrintWriter(out);
w.print(doc.getText(0, doc.getLength()));
w.close();
out.close();
voidtoXml(Document doc, OutputStream out)
Writes an xml representation to a stream.
try {
    DOMSource domSource = new DOMSource(doc);
    StreamResult result = new StreamResult(out);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.VERSION, doc.getXmlVersion());
    transformer.transform(domSource, result);
} catch (TransformerException e) {
    throw new IOException("unable to transform dom to a stream");
...
voidwrite(Document doc, OutputStream out, String enc)
Writes a DOM document to a stream.
if (enc == null) {
    throw new NullPointerException(
            "You must set an encoding; use \"UTF-8\" unless you have a good reason not to!"); 
Document doc2 = normalize(doc);
ClassLoader orig = Thread.currentThread().getContextClassLoader();
Thread.currentThread()
        .setContextClassLoader(AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { 
...
voidwrite(Document document, OutputStream byteStream)
Writes the given document to the given output stream.
DOMImplementationLS impl = (DOMImplementationLS) document.getImplementation();
LSOutput output = impl.createLSOutput();
output.setByteStream(byteStream);
LSSerializer serializer = impl.createLSSerializer();
serializer.getDomConfig().setParameter("format-pretty-print", true);
serializer.write(document, output);
voidwriteDocument(Document doc, OutputStream os)
write Document
try {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
    DOMSource source = new DOMSource(doc);
    CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder();
    StreamResult result = new StreamResult(new OutputStreamWriter(os, utf8Encoder));
...
OutputStreamwriteDocument(Document doc, OutputStream os)
write Document
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
    transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(os);
    transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
...