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 doc)

Converts a Document object into String representation

Element element = doc.getDocumentElement();
return elementToString(element);
StringdocumentToString(Document doc)
document To String
StringWriter stringWriter = new StringWriter();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(stringWriter);
Transformer transformer = getTransformerFactory().newTransformer();
transformer.transform(source, result);
return stringWriter.toString();
StringdocumentToString(Document doc)
document To String
StringWriter writer = new StringWriter();
writeDocument(doc, writer);
return writer.toString();
StringdocumentToString(Document doc)
Converts a DOM Document into a string.
DOMSource source = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory factory = TransformerFactory.newInstance();
factory.newTransformer().transform(source, result);
return writer.toString();
StringdocumentToString(Document doc)
document To String
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, result);
return writer.toString();
...
StringdocumentToString(Document doc)
Converts a DOM Document to a string.
StringWriter xmlStringWriter = new StringWriter();
transformerFactory.newTransformer().transform(new DOMSource(doc), new StreamResult(xmlStringWriter));
return xmlStringWriter.toString();
StringdocumentToString(Document doc)
document To String
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
...
StringdocumentToString(Document doc)
Helper method which converts XML Document into pretty formatted string
String strMsg = "";
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLSerializer serializer = new XMLSerializer(baos, format);
try {
    serializer.serialize(doc);
    strMsg = baos.toString("UTF-8");
...
StringdocumentToString(Document document)
document To String
try {
    int length = document.getLength();
    return document.getText(0, length);
} catch (BadLocationException e) {
    throw new RuntimeException(e);
StringdocumentToString(Document document)
document To String
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
document.setXmlStandalone(true);
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
StringWriter out = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(out));
...