Java XML Node Text Value getNodeText(Node node)

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

Description

Extracts to text from the supplied node and it's children.

License

Open Source License

Parameter

Parameter Description
node to extract text from.

Return

String representation of the node text.

Declaration

public static String getNodeText(Node node) 

Method Source Code


//package com.java2s;
/*/*from w  w  w .  ja  va 2 s .co  m*/
 * Copyright (c) 2004-2012 The YAWL Foundation. All rights reserved.
 * The YAWL Foundation is a collaboration of individuals and
 * organisations who are committed to improving workflow technology.
 *
 * This file is part of YAWL. YAWL is free software: you can
 * redistribute it and/or modify it under the terms of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 *
 * YAWL is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with YAWL. If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Extracts to text from the supplied node and it's children.
     *
     * @param node to extract text from.
     * @return String representation of the node text.
     */
    public static String getNodeText(Node node) {

        if (node == null || !node.hasChildNodes())
            return "";
        StringBuilder result = new StringBuilder();

        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node subnode = list.item(i);
            if (subnode.getNodeType() == Node.TEXT_NODE) {
                result.append(subnode.getNodeValue());
            } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) {
                result.append(subnode.getNodeValue());
            } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                // Recurse into the subtree for text
                // (and ignore comments)
                result.append(getNodeText(subnode));
            }
        }
        return result.toString();
    }
}

Related

  1. getInnerXmlText(Node xmlNode)
  2. getNamedItemText(NamedNodeMap map, String name)
  3. getNamespaceURIFromPrefix(Node context, String prefix)
  4. getNamespaceURIFromPrefix(Node context, String prefix)
  5. getNodeText(final Node node)
  6. getNodeText(Node node)
  7. getNodeText(Node node)
  8. getNodeText(Node node)
  9. getNodeText(Node node)