Java Utililty Methods XML DOM to String

List of utility methods to do XML DOM to String

Description

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

Method

StringdomNode2String(Node node, boolean indent)
Returns the String-Representation of the given DOM-Node as well-formed DOM-Document.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(node);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
return xmlString;
StringdomNodeListToString(NodeList nodeList)
dom Node List To String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    sb.append(domNodeToString(node));
return sb.toString();
StringdomNodeToString(Node node)
dom Node To String
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
StringdomToString(Node domNode)
Transforms a DOM node (e.g.
return domToString(domNode, false);
StringdomToString(Node domNode)
dom To String
Source source = new DOMSource(domNode);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(source, result);
return stringWriter.toString();
...
StringdomToString(Node domNode, boolean indent)
Convert specified DOM Node to a string representation
try {
    Source source = new DOMSource(domNode);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
...
StringdomToString(Node node)
dom To String
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
} catch (TransformerException te) {
...
StringdomToString(Node node, int estSize)
dom To String
String retVal = null;
if (node != null) {
    StringWriter sw = new StringWriter(estSize);
    xtrans.transform(new DOMSource(node), new StreamResult(sw));
    retVal = sw.toString();
    sw.close();
return retVal;
...