Java XML Node to String nodeToText(Node node)

Here you can find the source of nodeToText(Node node)

Description

node To Text

License

LGPL

Declaration

private static String nodeToText(Node node) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    private static String nodeToText(Node node) {
        StringBuffer buffer = new StringBuffer();
        if (node instanceof Text) {
            Text text = (Text) node;
            buffer.append(text.getData());
            buffer.append(' ');
        } else {/*  ww w  .jav a  2 s.c  o  m*/
            NodeList list = node.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                buffer.append(nodeToText(list.item(i)));
                buffer.append(' ');
            }
        }
        String ret = buffer.toString();
        return ret.replaceAll("\\s+", " ").trim();
    }
}

Related

  1. nodeToString(Node node, Set parentPrefixes, String namespaceURI)
  2. nodeToString(Node node, StringBuffer buf)
  3. nodeToString(Node node, StringBuffer sb)
  4. nodeToString(Object node)
  5. nodeToString(org.w3c.dom.Node domNode)
  6. nodeToWriter(Node node, Writer writer)
  7. nodeToXmlString(Node node)
  8. stringifyNode(Node node)
  9. toHtml(StringBuffer html, Node node)