Java XML Document to Stream writeXML(Document doc, OutputStream os)

Here you can find the source of writeXML(Document doc, OutputStream os)

Description

write out an XML file

License

Apache License

Parameter

Parameter Description
doc a parameter
os a parameter

Exception

Parameter Description
TransformerException an exception
IOException an exception

Declaration

public static void writeXML(Document doc, OutputStream os) throws TransformerException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.io.OutputStream;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    /**/*from w w w .  j a v  a  2 s .  co  m*/
     * write out an XML file
     * 
     * @param doc
     * @param os
     * @throws TransformerException 
     * @throws IOException 
     */
    public static void writeXML(Document doc, OutputStream os) throws TransformerException, IOException {
        // write out xml file
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        //indent XML properly
        //formatXML(doc,doc.getDocumentElement(),"  ");

        //normalize document
        doc.getDocumentElement().normalize();

        //write XML to file
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(os);

        transformer.transform(source, result);
        os.close();
    }
}

Related

  1. writeTransformedXml(Document doc, OutputStream output, InputStream style)
  2. writeXerces(Document doc, OutputStream out, String encoding)
  3. writeXHTML(Document htmldoc, OutputStream out)
  4. writeXML(Document d, OutputStream os, String sysID)
  5. writeXML(Document d, OutputStream out)
  6. writeXML(Document doc, OutputStream os)
  7. writeXML(Document doc, OutputStream out)
  8. writeXml(Document doc, OutputStream outputStream)
  9. writeXML(Document document, OutputStream os)