Java XML Document to String xmlDocumentToString(Document doc)

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

Description

xml Document To String

License

Open Source License

Declaration

public static String xmlDocumentToString(Document doc) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

import org.w3c.dom.*;

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;

public class Main {
    public static String xmlDocumentToString(Document doc) {
        try {//from ww w.j a v  a2s .  c o m
            StringWriter sw = new StringWriter();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    "no");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

            transformer.transform(new DOMSource(doc), new StreamResult(sw));
            return sw.toString();
        } catch (Exception ex) {
            return "";
        }
    }
}

Related

  1. writeXmlToString(Document xmlDoc)
  2. xmlDocToString(Document doc)
  3. xmlDocToString(Document doc)
  4. xmlDocumentDecode(final String xmlURI)
  5. xmlDocumentToString(Document doc)
  6. xmlDocumentToString(Document doc)
  7. xmlDocumentToString(Document document)
  8. xmlDocumentToString(final Document document)
  9. xmlDocumentToString(Node d)