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)
node To String
String textContent = node.getTextContent();
StringBuilder buf = new StringBuilder();
if (textContent != null) {
    short nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.TEXT_NODE: {
        buf.append(textContent);
        break;
...
StringnodeToString(Node node, boolean pretty)
See documentToString.
return nodeToString(node, (pretty ? 0 : -1));
StringnodeToString(Node node, Set parentPrefixes, String namespaceURI)
node To String
StringBuilder b = new StringBuilder();
if (node == null) {
    return "";
if (node instanceof Element) {
    Element element = (Element) node;
    b.append("<");
    b.append(element.getNodeName());
...
voidnodeToString(Node node, StringBuffer buf)
node To String
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource dSource = new DOMSource(node);
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
transformer.transform(dSource, sr);
StringWriter anotherSW = (StringWriter) sr.getWriter();
...
voidnodeToString(Node node, StringBuffer sb)
node To String
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE:
    sb.append("<?xml version=\"1.0\" ?>"); 
    nodeToString(((Document) node).getDocumentElement(), sb);
    break;
case Node.ELEMENT_NODE:
    sb.append("<"); 
...
StringnodeToString(Object node)
node To String
String result = "";
if (node instanceof String) {
    String str = ((String) node).replaceAll("\\s+", " ").trim();
    result += str;
} else if (node instanceof Text) {
    String str = ((Text) node).getTextContent().replaceAll("\\s+", " ").trim();
    result += str;
} else if (node instanceof Element) {
...
StringnodeToString(org.w3c.dom.Node domNode)
node To String
if (domNode == null) {
    return "";
String s = typeName[domNode.getNodeType()];
String nodeName = domNode.getNodeName();
if (!nodeName.startsWith("#")) {
    s += ": " + nodeName;
if (domNode.getNodeValue() != null) {
    if (s.startsWith("ProcInstr"))
        s += ", ";
    else
        s += ": ";
    String t = domNode.getNodeValue().trim();
    int x = t.indexOf("\n");
    if (x >= 0)
        t = t.substring(0, x);
    s += t;
return s;
StringnodeToText(Node node)
node To Text
StringBuffer buffer = new StringBuffer();
if (node instanceof Text) {
    Text text = (Text) node;
    buffer.append(text.getData());
    buffer.append(' ');
} else {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
...
voidnodeToWriter(Node node, Writer writer)
node To Writer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
DOMSource source = new DOMSource(node);
StreamResult result = new StreamResult(writer);
...
StringnodeToXmlString(Node node)
Returns an XML representation of the provided node.
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(node);
    StreamResult result = new StreamResult(new StringWriter());
    t.transform(source, result);
    return result.getWriter().toString();
} catch (TransformerException e) {
...