Java XML Document to String xmlToString(Document xml)

Here you can find the source of xmlToString(Document xml)

Description

Method to transform an XML document into a pretty-formatted string.

License

Apache License

Parameter

Parameter Description
xml a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static final String xmlToString(Document xml) throws Exception 

Method Source Code

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

import java.io.StringWriter;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
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 {
    /**// www  .  ja  v a2 s.  c  om
     * Method to transform an XML document into a pretty-formatted string.
     * @param xml
     * @return
     * @throws Exception
     */
    public static final String xmlToString(Document xml) throws Exception {
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        Writer out = new StringWriter();
        tf.transform(new DOMSource(xml), new StreamResult(out));
        return out.toString();
    }
}

Related

  1. xmlToString(Document doc)
  2. XMLtoString(Document doc)
  3. xmlToString(Document doc)
  4. xmlToString(Document document)
  5. xmlToString(Document xml)
  6. XMLToString(Document XMLDocument)
  7. xmltoString(final Document document)
  8. xmlToStringBuffer(Document doc, int identAmount)