Java XML Document to String documentToString(Document document)

Here you can find the source of documentToString(Document document)

Description

document To String

License

Apache License

Declaration

public static String documentToString(Document document) 

Method Source Code

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

import java.io.StringWriter;

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 {
    private static TransformerFactory tf;

    public static String documentToString(Document document) {
        try {/* www . j a  va  2 s .  c o m*/
            Transformer trans = tf.newTransformer();
            StringWriter sw = new StringWriter();
            trans.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString();
        } catch (TransformerException tEx) {
            tEx.printStackTrace();
        }
        return null;
    }

    public static String documentToString(Document document, boolean omitXmlDeclaration) {
        try {
            Transformer trans = tf.newTransformer();
            trans.setOutputProperty("omit-xml-declaration", omitXmlDeclaration ? "yes" : "no");
            StringWriter sw = new StringWriter();
            trans.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString();
        } catch (TransformerException tEx) {
            tEx.printStackTrace();
        }
        return null;
    }
}

Related

  1. documentToString(Document document)
  2. documentToString(Document document)
  3. documentToString(Document document)
  4. documentToString(Document document)
  5. documentToString(Document document)
  6. documentToString(Document document, boolean pretty)
  7. documentToString(Document document, boolean standalone)
  8. documentToString(Document document, Transformer documentTransformer)
  9. DocumentToString(Document dom)