Drop in replacement for XML Node.getTextContent(). - Java XML

Java examples for XML:XML Node Text

Description

Drop in replacement for XML Node.getTextContent().

Demo Code


//package com.java2s;

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

public class Main {
    /**/*  www . jav 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 Tutorials