Java XML Document Serialize serialize(Document doc, Writer out)

Here you can find the source of serialize(Document doc, Writer out)

Description

serialize

License

Apache License

Declaration

public static void serialize(Document doc, Writer out)
            throws UnsupportedEncodingException, TransformerException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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 {
    public static void serialize(Document doc, Writer out)
            throws UnsupportedEncodingException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        transformer.transform(new DOMSource(doc), new StreamResult(out));
    }//from w  ww. j a  v a 2s.c  om

    public static void serialize(Document doc, OutputStream out)
            throws UnsupportedEncodingException, TransformerException {
        serialize(doc, new OutputStreamWriter(out, "UTF-8"));
    }
}

Related

  1. serialise(Document doc, OutputStream out)
  2. serialiseDOM(Document doc, String filepath)
  3. serialize(Document d, File f)
  4. serialize(Document d, OutputStream out, URL transformerLocation, Map properties)
  5. serialize(Document doc)
  6. serialize(Document document, boolean prettyPrint)
  7. serialize(Document document, boolean prettyPrint)
  8. serialize(Document document, File targetFile)
  9. serialize(Document document, OutputStream ostream)