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

StringgetXMLString(Node node, boolean omitXmlDeclaration)
Returns the XML string representation of a Node.
StringWriter writer = new StringWriter();
DOMSource domSource = new DOMSource(node);
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
serializer.transform(domSource, result);
...
StringgetXMLStringFragmentFromNode(Node node)
Takes the supplied node as the root of an xml document and converts it to a String representation.
return getXMLStringFragmentFromNode(node, true);
StringnodeToStr(final Node node)
node To Str
final String YES = "yes";
StringWriter sw = new StringWriter();
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, YES);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES);
    transformer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
...
StringnodeToString(final Node node)
Convert XML Node to String
final Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
final Writer out = new StringWriter();
tf.transform(new DOMSource(node), new StreamResult(out));
return out.toString();
StringnodeToString(final Node node)
node To String
return nodeToString(node, false);
StringnodeToString(final Node node)
node To String
final TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
    transformer = transFactory.newTransformer();
    final StringWriter buffer = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(buffer));
    buffer.append("\n");
...
StringnodeToString(final Node node)
Converts a Node node to an XML string
try {
    final StringWriter writer = new StringWriter();
    createIndentingTransformer().transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
} catch (final TransformerException e) {
    throw new IllegalStateException(e);
StringnodeToString(Node doc)
node To String
try {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
...
StringnodeToString(Node n)
Generates an xml string from a node (not pretty formatted)
StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");
transformer.setOutputProperty("indent", "yes");
transformer.transform(new DOMSource(n), new StreamResult(sw));
return sw.toString();
StringnodeToString(Node n)
node To String
return nodeToString(n, true, true);