Here you can find the source of writeDocumentToStreamResult(Document document, StreamResult outputTarget)
private static void writeDocumentToStreamResult(Document document, StreamResult outputTarget)
//package com.java2s; /*/*w ww.j a va2s . c o m*/ * Copyright 2016-2017 the original author or authors. * * 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. */ import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { private static final ThreadLocal<TransformerFactory> TRANSFORMER_FACTORY = new ThreadLocal<TransformerFactory>() { @Override protected TransformerFactory initialValue() { return TransformerFactory.newInstance(); } }; private static void writeDocumentToStreamResult(Document document, StreamResult outputTarget) { try { Transformer transformer = TRANSFORMER_FACTORY.get().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(document), outputTarget); } catch (TransformerConfigurationException ex) { throw new RuntimeException(ex); } catch (TransformerException ex) { throw new RuntimeException(ex); } } }