Here you can find the source of serialize(final Document doc, final OutputStream os, final String encoding)
static void serialize(final Document doc, final OutputStream os, final String encoding) throws TransformerFactoryConfigurationError, TransformerException, IOException
//package com.java2s; /*/*ww w . ja va 2s . co m*/ * #%L * xcode-maven-plugin * %% * Copyright (C) 2012 SAP AG * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { /** * Serializes a DOM. The OutputStream handed over to this method is not closed inside this method. */ static void serialize(final Document doc, final OutputStream os, final String encoding) throws TransformerFactoryConfigurationError, TransformerException, IOException { if (doc == null) throw new IllegalArgumentException("No document provided."); if (os == null) throw new IllegalArgumentException("No output stream provided"); if (encoding == null || encoding.isEmpty()) throw new IllegalArgumentException("No encoding provided."); final TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", Integer.valueOf(2)); final Transformer t = transformerFactory.newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.ENCODING, encoding); final OutputStreamWriter osw = new OutputStreamWriter(os, encoding); t.transform(new DOMSource(doc), new StreamResult(osw)); osw.flush(); } }