Java XML Document Serialize serialize(Document document, Writer writer)

Here you can find the source of serialize(Document document, Writer writer)

Description

serialize

License

Open Source License

Declaration

static void serialize(Document document, Writer writer) 

Method Source Code


//package com.java2s;
/*// www .  j  a v a 2s.  co  m
 * Copyright 2011 StackFrame, LLC
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.Writer;
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 {
    static void serialize(Document document, Writer writer) {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", new Integer(4));
        try {
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            StreamResult result = new StreamResult(writer);
            transformer.transform(new DOMSource(document), result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. serialize(Document document, boolean prettyPrint)
  2. serialize(Document document, File targetFile)
  3. serialize(Document document, OutputStream ostream)
  4. serialize(Document document, OutputStream out)
  5. serialize(Document document, String fileName)
  6. serialize(final Document doc, final OutputStream os, final String encoding)
  7. serialize(final Document document, final OutputStream out)
  8. serializeDocument(Document doc)
  9. serializeDocument(Document doc, String filePath)