Java XML Document to String writeDocumentToStreamResult(Document document, StreamResult outputTarget)

Here you can find the source of writeDocumentToStreamResult(Document document, StreamResult outputTarget)

Description

write Document To Stream Result

License

Apache License

Declaration

private static void writeDocumentToStreamResult(Document document, StreamResult outputTarget) 

Method Source Code

//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);
        }
    }
}

Related

  1. write2xml(Document document)
  2. write2Xml(Document document)
  3. write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)
  4. writeDocument(Document doc, boolean omitXmlDeclaration)
  5. writeDocument(Document document)
  6. writeToBytes(Document document)
  7. writeXML(Document d)
  8. writeXmlToString(Document xmlDoc)
  9. xmlDocToString(Document doc)