Java XML Document to String document2String(Document doc)

Here you can find the source of document2String(Document doc)

Description

document String

License

LGPL

Declaration

public static String document2String(Document doc)
            throws ParserConfigurationException, TransformerConfigurationException, TransformerException 

Method Source Code


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

import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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 {
    public static String document2String(Document doc)
            throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
        DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = domFact.newDocumentBuilder();
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    }/* ww w .ja va 2  s  . co m*/
}

Related

  1. document2String(Document doc, boolean prettyPrint)
  2. document2String(Node document)
  3. document2XmlString(Document xmldoc)
  4. documentToString(Document d)