Java XML Document to String documentToString(Node document)

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

Description

document To String

License

Open Source License

Declaration

public static String documentToString(Node document) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Node;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.StringWriter;

public class Main {
    public static String documentToString(Node document) {
        try {/*from   ww w .  j a v a  2  s .  c o  m*/
            StringWriter sw = new StringWriter();
            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.ENCODING, "UTF-8");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString().replace("\r\n", "\n");
        } catch (TransformerException e) {
            throw new IllegalAccessError("Couldn't transform document to string");
        }
    }
}

Related

  1. documentToString(Document document, boolean pretty)
  2. documentToString(Document document, boolean standalone)
  3. documentToString(Document document, Transformer documentTransformer)
  4. DocumentToString(Document dom)
  5. documentToString(final Node node)
  6. dom2String2(Document document)
  7. DOMaXML(Document doc, String nome)
  8. domToString(Document document)
  9. domToString(final Document document)