Java XML Document to String domToString(final Document document)

Here you can find the source of domToString(final Document document)

Description

dom To String

License

BSD License

Declaration

private static String domToString(final Document document) throws Exception 

Method Source Code


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

import java.io.StringWriter;

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 {
    private static String domToString(final Document document) throws Exception {
        String result = null;/*  w  ww. ja va  2s .c o  m*/
        StringWriter strWtr = new StringWriter();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        if (document != null) {
            StreamResult strResult = new StreamResult(strWtr);
            transformer.transform(new DOMSource(document.getDocumentElement()), strResult);
            result = strResult.getWriter().toString();
        }
        return result;
    }
}

Related

  1. documentToString(final Node node)
  2. documentToString(Node document)
  3. dom2String2(Document document)
  4. DOMaXML(Document doc, String nome)
  5. domToString(Document document)
  6. getDocumentAsString(Document doc)
  7. getDocumentAsString(Document doc)
  8. getDocumentAsString(Document document)
  9. getDocumentAsString(Node node, boolean prettyXml)