Here you can find the source of serialize(Document d, OutputStream out, URL transformerLocation, Map
public static void serialize(Document d, OutputStream out, URL transformerLocation, Map<String, String> properties) throws TransformerException, IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.URL; import java.util.Map; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; 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 javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; public class Main { static final TransformerFactory tFactory = TransformerFactory.newInstance(); public static void serialize(Document d, OutputStream out, URL transformerLocation, Map<String, String> properties) throws TransformerException, IOException { Source source = new DOMSource(d); Transformer transformer = null; if (transformerLocation != null) { Source stylesource = new StreamSource(transformerLocation.toString()); transformer = tFactory.newTransformer(stylesource); } else//from ww w .j a v a2s .co m transformer = tFactory.newTransformer(); String documentEncoding = d.getXmlEncoding(); if (documentEncoding == null) documentEncoding = "UTF-8"; transformer.setOutputProperty(OutputKeys.ENCODING, documentEncoding); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); DocumentType dt = d.getDoctype(); //If no transformation, we try to keep the Document type if (dt != null && transformerLocation == null) { String publicId = dt.getPublicId(); if (publicId != null) transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicId); String systemId = dt.getSystemId(); if (systemId != null) transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemId); } if (properties != null) { for (Map.Entry<String, String> e : properties.entrySet()) { transformer.setOutputProperty(e.getKey(), e.getValue()); } } Writer w = new OutputStreamWriter(out, documentEncoding); StreamResult result = new StreamResult(w); transformer.transform(source, result); out.flush(); } }