XML To String - Java XML

Java examples for XML:XML Transform

Description

XML To String

Demo Code


import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.OutputKeys;
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 XMLToString(Document doc) {
        try {//from  w  ww.  j  ava2  s . c om
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(
                    "{http://xml.apache.org/xslt}indent-amount", "4");
            DOMSource source = new DOMSource(doc);
            StringWriter stw = new StringWriter();
            transformer.transform(source, new StreamResult(stw));
            return stw.toString();
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return "";
    }
}

Related Tutorials