org.w3c.dom.Document to XML String - Java XML

Java examples for XML:DOM Document

Description

org.w3c.dom.Document to XML String

Demo Code


//package com.java2s;
import java.io.StringWriter;
import javax.xml.parsers.ParserConfigurationException;
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;

public class Main {
    /**/* w  w w.j a v  a  2s.com*/
     * org.w3c.dom.Document to XML String
     */
    public static String toXML(org.w3c.dom.Document w3cDoc)
            throws ParserConfigurationException, TransformerException {
        final DOMSource source = new DOMSource(w3cDoc);
        final StringWriter writer = new StringWriter();
        final StreamResult result = new StreamResult(writer);
        final Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        transformer.transform(source, result);
        return writer.toString();
    }
}

Related Tutorials