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

StringnodeToString(Node node)
Serialized a node to a string.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
StringnodeToString(Node node)
node To String
StringWriter sw = new StringWriter();
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
    throw new RuntimeException(te);
return sw.toString();
StringnodeToString(Node node)
node To String
DOMSource source = new DOMSource(node);
return convertDOMToString(source, true);
StringnodeToString(Node node)
node To String
StringBuilder s = new StringBuilder(node.getNodeName() + "( ");
NamedNodeMap map = node.getAttributes();
if (map != null) {
    for (int i = 0; i < map.getLength(); i++) {
        s.append(map.item(i).getNodeName()).append("=").append(map.item(i).getNodeValue()).append(" ");
return s.append(" )").toString();
...
StringnodeToString(Node node)
node To String
StringWriter s = new StringWriter();
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(new DOMSource(node), new StreamResult(s));
...
StringnodeToString(Node node)
node To String
StringWriter sw = new StringWriter();
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
    throw new RuntimeException(te);
...
StringnodeToString(Node node)
node To String
try {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    Transformer transformer = threadLocalTransformer.get();
    transformer.reset();
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
...
StringnodeToString(Node node)
Get standard xml string of node
StringWriter sw = new StringWriter();
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException e) {
    e.printStackTrace();
...
StringnodeToString(Node node)
node To String
StringWriter sw = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
StringnodeToString(Node node)
node To String
try {
    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(node);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
    return writer.toString();
} catch (Exception ex) {
...