Java XML Node Text Value getText(Node nd)

Here you can find the source of getText(Node nd)

Description

Drop in replacement for Node.getTextContent().

License

Apache License

Declaration

public static String getText(Node nd) 

Method Source Code

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

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

public class Main {
    /**/*from   ww  w  .  j  a v  a  2 s  .  c om*/
     * Drop in replacement for Node.getTextContent().
     *
     * <p>
     * This method is provided to support the same functionality as
     * Node.getTextContent() but in a way that is DOM Level 2 compatible.
     * </p>
     *
     * @see <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent">DOM Object Model Core</a>
     */
    public static String getText(Node nd) {

        short type = nd.getNodeType();

        // for most node types, we can defer to the recursive helper method,
        // but when asked for the text of these types, we must return null
        // (Not the empty string)
        switch (type) {

        case Node.DOCUMENT_NODE: /* fall through */
        case Node.DOCUMENT_TYPE_NODE: /* fall through */
        case Node.NOTATION_NODE: /* fall through */
            return null;
        }

        StringBuilder sb = new StringBuilder();
        getText(nd, sb);
        return sb.toString();
    }

    /** @see #getText(Node) */
    private static void getText(Node nd, StringBuilder buf) {

        short type = nd.getNodeType();

        switch (type) {

        case Node.ELEMENT_NODE: /* fall through */
        case Node.ENTITY_NODE: /* fall through */
        case Node.ENTITY_REFERENCE_NODE: /* fall through */
        case Node.DOCUMENT_FRAGMENT_NODE:
            NodeList childs = nd.getChildNodes();
            for (int i = 0; i < childs.getLength(); i++) {
                Node child = childs.item(i);
                short childType = child.getNodeType();
                if (childType != Node.COMMENT_NODE && childType != Node.PROCESSING_INSTRUCTION_NODE) {
                    getText(child, buf);
                }
            }
            break;

        case Node.ATTRIBUTE_NODE: /* fall through */
            /* Putting Attribute nodes in this section does not exactly
               match the definition of how textContent should behave
               according to the DOM Level-3 Core documentation - which
               specifies that the Attr's children should have their
               textContent concated (Attr's can have a single child which
               is either Text node or an EntityRefrence).  In practice,
               DOM implementations do not seem to use child nodes of
               Attributes, storing the "text" directly as the nodeValue.
               Fortunately, the DOM Spec indicates that when Attr.nodeValue
               is read, it should return the nodeValue from the child Node,
               so this approach should work both for strict implementations,
               and implementations actually encountered.
            */
        case Node.TEXT_NODE: /* fall through */
        case Node.CDATA_SECTION_NODE: /* fall through */
        case Node.COMMENT_NODE: /* fall through */
        case Node.PROCESSING_INSTRUCTION_NODE: /* fall through */
            buf.append(nd.getNodeValue());
            break;

        case Node.DOCUMENT_NODE: /* fall through */
        case Node.DOCUMENT_TYPE_NODE: /* fall through */
        case Node.NOTATION_NODE: /* fall through */
        default:
            /* :NOOP: */

        }
    }
}

Related

  1. getText(Node elem)
  2. getText(Node elem, StringBuilder buffer)
  3. getText(Node n)
  4. getText(Node n)
  5. getText(Node n)
  6. getText(Node nd, StringBuilder buf)
  7. getText(Node node)
  8. getText(Node node)
  9. getText(Node node)