Java Utililty Methods XML Node to String

List of utility methods to do XML Node to String

Description

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

Method

StringstringifyNode(Node node)
stringify Node
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(node);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Result result = new StreamResult(out);
    transformer.transform(source, result);
    return out.toString();
} catch (TransformerConfigurationException e) {
...
voidtoHtml(StringBuffer html, Node node)
_more_
switch (node.getNodeType()) {
case Node.ELEMENT_NODE: {
    NodeList children = node.getChildNodes();
    int numChildren = children.getLength();
    html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>");
    html.append(": ");
    for (int i = 0; i < numChildren; i++) {
        Node child = children.item(i);
...
StringtoString(final Node node)
Transform a DOM Node into an XML String.
return toString(node, true);
StringtoString(final Node node)
Transform a DOM Node to String.
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter writer = new StringWriter();
DOMSource source = new DOMSource(node);
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
return writer.toString();
StringtoString(final Node node)
Returns the content of the given node as string
final StringWriter out = new StringWriter();
write(node, new StreamResult(out));
return out.toString();
StringtoString(final Node xml)
to String
try {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    final StreamResult result = new StreamResult(new StringWriter());
    final DOMSource source = new DOMSource(xml);
    transformer.transform(source, result);
    return result.getWriter().toString();
...
StringtoString(final short nodeType)
to String
return toClass(nodeType).getSimpleName();
StringtoString(Node element)
to String
if (element == null) {
    return "null";
Source source = new DOMSource(element);
StringWriter stringWriter = new StringWriter();
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
    Result result = new StreamResult(printWriter);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
...
StringtoString(Node n)
to String
StringBuilder buf = new StringBuilder();
append(n, buf, 0);
return buf.toString();
StringtoString(Node n)
to String
TransformerHandler hd = newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(n);
StringWriter w = new StringWriter();
StreamResult result = new StreamResult(w);
serializer.transform(source, result);
...