Java XML Document to String write(String filename, Document document, boolean addDocType)

Here you can find the source of write(String filename, Document document, boolean addDocType)

Description

write

License

Open Source License

Declaration

public static boolean write(String filename, Document document, boolean addDocType) 

Method Source Code


//package com.java2s;
import java.io.BufferedWriter;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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 {
    public static boolean write(String filename, Document document, boolean addDocType) {
        try {/*from  ww w . ja v a 2  s  .  com*/
            TransformerFactory tf = TransformerFactory.newInstance();
            tf.setAttribute("indent-number", new Integer(4));
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(document);
            if (addDocType) {
                transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                        "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN");
                transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                        "http://java.sun.com/dtd/facelet-taglib_1_0.dtd");
            }
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(source,
                    new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)))));
            return true;
        } catch (Exception e) {
            return false;
        }

    }
}

Related

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