Java XML Document to String write(Document doc, Result result)

Here you can find the source of write(Document doc, Result result)

Description

write

License

Apache License

Declaration

static public void write(Document doc, Result result) 

Method Source Code

//package com.java2s;
/*//from  www.  java  2s  .  c om
 * Copyright (C) 2010 Trustees of the University of Pennsylvania
 *
 * 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 java.io.OutputStream;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
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 {
    static public void write(Document doc, Writer out) {
        write(doc, new StreamResult(out));
    }

    static public void write(Document doc, OutputStream out) {
        write(doc, new StreamResult(out));
    }

    static public void write(Document doc, Result result) {
        TransformerFactory tfact = TransformerFactory.newInstance();
        try {
            Transformer trans = tfact.newTransformer();
            trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
            // text
            trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            trans.transform(new DOMSource(doc.getDocumentElement()), result);
        } catch (TransformerConfigurationException e) {
            assert (false); // can't happen
        } catch (TransformerException e) {
            assert (false); // can't happen
        }
    }
}

Related

  1. toXmlString(Document doc)
  2. toXmlString(Document doc)
  3. toXMLString(Document doc, boolean includeXMLDecl, boolean indent)
  4. toXmlString(Document document)
  5. write(Document doc)
  6. write(String filename, Document document, boolean addDocType)
  7. write2xml(Document document)
  8. write2Xml(Document document)
  9. write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)