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

voidwriteTransformedXml(Document doc, OutputStream output, InputStream style)
write Transformed Xml
TransformerFactory transformerFactory = TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource(style);
Transformer transformer = transformerFactory.newTransformer(stylesource);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(output);
transformer.transform(source, result);
...
voidwriteXerces(Document doc, OutputStream out, String encoding)
Serialize a document using Xerces' library.
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class xmlSerializerClazz = Class.forName("org.apache.xml.serialize.XMLSerializer", true, cl); 
Class outputFormatClazz = Class.forName("org.apache.xml.serialize.OutputFormat", true, cl); 
Object xmlSerializer = xmlSerializerClazz.newInstance();
Object outputFormat = outputFormatClazz.newInstance();
Method setMethod = outputFormatClazz.getMethod("setMethod", new Class[] { String.class }); 
setMethod.invoke(outputFormat, new Object[] { "xml" }); 
Method setIndenting = outputFormatClazz.getMethod("setIndenting", new Class[] { Boolean.TYPE }); 
...
voidwriteXHTML(Document htmldoc, OutputStream out)
Write an HTML document to an output stream.
javax.xml.transform.Result result = new StreamResult(out);
Transformer xformer;
try {
    xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, HTML_XHTML_TRANSITIONAL_DOCTYPE_PUBLIC);
    xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, HTML_XHTML_TRANSITIONAL_DOCTYPE_SYSTEM);
...
voidwriteXML(Document d, OutputStream os, String sysID)
write XML
PrintWriter out = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println();
if (sysID != null) {
    out.println("<!DOCTYPE " + d.getDoctype().getName() + " SYSTEM \"" + sysID + "\">");
    out.println();
writeXMLwalkTree(d.getDocumentElement(), 0, out);
...
voidwriteXML(Document d, OutputStream out)
Write the XML-encoding of a Document Document to an java.io.OutputStream OutputStream .
DOMImplementationLS DOMiLS = (DOMImplementationLS) d.getImplementation();
;
LSOutput LSO = DOMiLS.createLSOutput();
LSO.setByteStream(out);
LSSerializer LSS = DOMiLS.createLSSerializer();
if (!LSS.write(d, LSO)) {
    throw new IOException("[Serialization failed!]");
voidwriteXML(Document doc, OutputStream os)
write out an 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");
doc.getDocumentElement().normalize();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
...
voidwriteXML(Document doc, OutputStream os)
write out an XML file
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
formatXML(doc, doc.getDocumentElement(), "  ");
doc.getDocumentElement().normalize();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
os.close();
...
voidwriteXML(Document doc, OutputStream out)
Escribe un Document en un outputstream
OutputStreamWriter osw = new OutputStreamWriter(out);
com.sun.org.apache.xerces.internal.dom.DOMOutputImpl domoutputimpl = new com.sun.org.apache.xerces.internal.dom.DOMOutputImpl();
domoutputimpl.setEncoding(doc.getXmlEncoding());
domoutputimpl.setCharacterStream(osw);
org.w3c.dom.ls.LSSerializer serializer;
org.w3c.dom.ls.DOMImplementationLS dils;
dils = (org.w3c.dom.ls.DOMImplementationLS) doc.getImplementation();
serializer = dils.createLSSerializer();
...
voidwriteXml(Document doc, OutputStream outputStream)
write Xml
try {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(outputStream);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    try {
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
    } catch (IllegalArgumentException e) { 
    try {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } catch (IllegalArgumentException e) { 
    xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
    throw new IllegalStateException(e);
} catch (TransformerException e) {
    throw new IllegalStateException(e);
voidwriteXML(Document document, OutputStream os)
Saves a given XML document to the given output stream.
DOMSource src = new DOMSource(document);
StreamResult res = new StreamResult(os);
TransformerFactory tf = TransformerFactory.newInstance();
try {
    Transformer t = tf.newTransformer();
    t.transform(src, res);
} catch (TransformerException e) {
    throw new IOException(e.getMessage());
...