Java XML Document to Stream writeXerces(Document doc, OutputStream out, String encoding)

Here you can find the source of writeXerces(Document doc, OutputStream out, String encoding)

Description

Serialize a document using Xerces' library.

License

Sun Public License Notice

Declaration

private static void writeXerces(Document doc, OutputStream out, String encoding)
        throws ClassNotFoundException, Exception 

Method Source Code

//package com.java2s;
/*/*from  w  ww .  ja v a 2 s.  c o m*/
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

import java.io.OutputStream;

import java.lang.reflect.Method;

import org.w3c.dom.Document;

public class Main {
    /**
     * Serialize a document using Xerces' library.
     * This library is available in the JDK starting with version 1.5 (where we do not need it anyway),
     * but in 1.4 you need to have Xerces loaded in the system somewhere.
     */
    private static void writeXerces(Document doc, OutputStream out, String encoding)
            throws ClassNotFoundException, Exception {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Class xmlSerializerClazz = Class.forName("org.apache.xml.serialize.XMLSerializer", true, cl); // NOI18N
        Class outputFormatClazz = Class.forName("org.apache.xml.serialize.OutputFormat", true, cl); // NOI18N
        Object xmlSerializer = xmlSerializerClazz.newInstance();
        Object outputFormat = outputFormatClazz.newInstance();
        Method setMethod = outputFormatClazz.getMethod("setMethod", new Class[] { String.class }); // NOI18N
        setMethod.invoke(outputFormat, new Object[] { "xml" }); // NOI18N
        Method setIndenting = outputFormatClazz.getMethod("setIndenting", new Class[] { Boolean.TYPE }); // NOI18N
        setIndenting.invoke(outputFormat, new Object[] { Boolean.TRUE }); // NOI18N
        Method setLineWidth = outputFormatClazz.getMethod("setLineWidth", new Class[] { Integer.TYPE }); // NOI18N
        setLineWidth.invoke(outputFormat, new Object[] { new Integer(0) });
        Method setLineSeparator = outputFormatClazz.getMethod("setLineSeparator", new Class[] { String.class }); // NOI18N
        setLineSeparator.invoke(outputFormat, new String[] { System.getProperty("line.separator") }); // NOI18N
        Method setOutputByteStream = xmlSerializerClazz.getMethod("setOutputByteStream",
                new Class[] { OutputStream.class }); // NOI18N
        setOutputByteStream.invoke(xmlSerializer, new Object[] { out });
        Method setEncoding = outputFormatClazz.getMethod("setEncoding", new Class[] { String.class }); // NOI18N
        setEncoding.invoke(outputFormat, new Object[] { encoding });
        Method setOutputFormat = xmlSerializerClazz.getMethod("setOutputFormat", new Class[] { outputFormatClazz }); // NOI18N
        setOutputFormat.invoke(xmlSerializer, new Object[] { outputFormat });
        Method setNamespaces = xmlSerializerClazz.getMethod("setNamespaces", new Class[] { Boolean.TYPE }); // NOI18N
        setNamespaces.invoke(xmlSerializer, new Object[] { Boolean.TRUE });
        Method asDOMSerializer = xmlSerializerClazz.getMethod("asDOMSerializer", new Class[0]); // NOI18N
        Object impl = asDOMSerializer.invoke(xmlSerializer, null);
        Method serialize = impl.getClass().getMethod("serialize", new Class[] { Document.class }); // NOI18N
        serialize.invoke(impl, new Object[] { doc });
    }
}

Related

  1. writeDocument(OutputStream os, Node node)
  2. writeDocument(OutputStream stream, Document document)
  3. writeDOMDocumentToStream(Document doc, OutputStream stream)
  4. writeOutDocument(OutputStream os, Document doc)
  5. writeTransformedXml(Document doc, OutputStream output, InputStream style)
  6. writeXHTML(Document htmldoc, OutputStream out)
  7. writeXML(Document d, OutputStream os, String sysID)
  8. writeXML(Document d, OutputStream out)
  9. writeXML(Document doc, OutputStream os)