Here you can find the source of writeXerces(Document doc, OutputStream out, String encoding)
private static void writeXerces(Document doc, OutputStream out, String encoding) throws ClassNotFoundException, Exception
//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 }); } }