Java XML Document Serialize serializeToString(org.w3c.dom.Document doc)

Here you can find the source of serializeToString(org.w3c.dom.Document doc)

Description

serialize To String

License

BSD License

Declaration

public static String serializeToString(org.w3c.dom.Document doc) 

Method Source Code

//package com.java2s;
/*/*from w w  w.  j  a  v  a 2s  .  co m*/
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

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 java.io.*;

public class Main {
    public static String serializeToString(org.w3c.dom.Document doc) {
        try {
            DOMSource domSource = new DOMSource(doc);
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "no");
            transformer.transform(domSource, result);
            writer.flush();
            return writer.toString();
        } catch (TransformerException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Related

  1. serializeDocument(Document document)
  2. serializeDocument(Document document, OutputStream os)
  3. serializeDocument(Document root)
  4. serializeDOM(Document doc)
  5. serializeToByteArray(Document doc)
  6. serializeToStringLS(Document doc, Node node, String encoding)
  7. serializeXML(Document doc, Writer writer, boolean addXmlDeclaration)
  8. serializeXmlObject(org.w3c.dom.Document docToSerialize)
  9. serializeXMLToFile(Document document, File output)