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

StringxmlToString(Document xml)
Method to transform an XML document into a pretty-formatted string.
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();
StringXMLToString(Document XMLDocument)
Converts DOM document to a string
String xmlString = null;
try {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    Transformer trans = TransformerFactory.newInstance().newTransformer();
    trans.transform(new DOMSource(XMLDocument), result);
    xmlString = sw.toString();
    sw.close();
...
StringxmltoString(final Document document)
xmlto String
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.transform(new DOMSource(document.getDocumentElement()), streamResult);
...
StringBufferxmlToStringBuffer(Document doc, int identAmount)
xml To String Buffer
final TransformerFactory transfac = TransformerFactory.newInstance();
final Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(identAmount));
final StringWriter sw = new StringWriter();
final StreamResult result = new StreamResult(sw);
final DOMSource source = new DOMSource(doc);
...