Java Utililty Methods XML Document to String

List of utility methods to do XML Document to String

Description

The list of methods to do XML Document to String are organized into topic(s).

Method

StringdocumentToString(Document document)
Uses a TransformerFactory with an identity transformation to convert a Document into a String representation of the XML.
String xml = null;
try {
    DOMSource dom = new DOMSource(document);
    StringWriter writer = new StringWriter();
    StreamResult output = new StreamResult(writer);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, INDENT_XML);
...
StringdocumentToString(Document document)
document To String
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
    transformer = tf.newTransformer();
} catch (TransformerConfigurationException e) {
    throw new RuntimeException(e);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
...
StringdocumentToString(Document document)
document To String
try {
    Transformer trans = tf.newTransformer();
    StringWriter sw = new StringWriter();
    trans.transform(new DOMSource(document), new StreamResult(sw));
    return sw.toString();
} catch (TransformerException tEx) {
    tEx.printStackTrace();
return null;
StringdocumentToString(Document document)
document To String
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    StringWriter sw = new StringWriter();
    trans.transform(new DOMSource(document), new StreamResult(sw));
    return sw.toString();
} catch (TransformerException tEx) {
    tEx.printStackTrace();
...
StringdocumentToString(Document document)
document To String
return documentToString(document, true);
StringdocumentToString(Document document, boolean pretty)
Convert the given Document to an XML String.
if (document == null) {
    return "";
return nodeToString(document.getFirstChild(), pretty);
StringdocumentToString(Document document, boolean standalone)
document To String
String prol = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"" + (standalone ? "yes" : "no")
        + "\"?>";
return prol + nodeToString(document.getDocumentElement(), new HashSet(),
        document.getDocumentElement().getNamespaceURI());
StringdocumentToString(Document document, Transformer documentTransformer)
Converts a document object to an xml string
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(document);
documentTransformer.transform(source, result);
return sw.toString();
StringDocumentToString(Document dom)
Parses org.w3c.dom.Document and returns its xml serialization as String
String xmlString = null;
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(dom);
    transformer.transform(source, result);
    xmlString = result.getWriter().toString();
...
StringdocumentToString(final Node node)
Converts the given document to string format by passing it through a transformer.
final TransformerFactory transformerFactory;
final Transformer transformer;
final DOMSource source;
final StreamResult result;
final StringWriter writer;
writer = new StringWriter(BUFFER_CAPACITY);
transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
...