Java XML Node Text Value getTextForNode(Node node)

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

Description

Gets the String value of the node.

License

Open Source License

Parameter

Parameter Description
node the node of interest

Return

the value of that node

Declaration

public static String getTextForNode(Node node) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from ww w.ja  va2 s  .  c  o  m*/
     * Gets the String value of the node. 
     If the node does not contain text then an empty String is returned
     * @param node the node of interest
     * @return the value of that node
     */
    public static String getTextForNode(Node node) {
        NodeList children = node.getChildNodes();
        if (children == null) {
            return "";
        }

        for (int i = 0; i < children.getLength(); i++) {
            Node childNode = children.item(i);
            if ((childNode.getNodeType() == Node.TEXT_NODE)
                    || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
                return childNode.getNodeValue();
            }
        }

        return "";
    }
}

Related

  1. getTextContent(org.w3c.dom.Node element)
  2. getTextContentOld(final Node node)
  3. getTextContents(Node node)
  4. getTextData(Node element)
  5. getTextData(Node node)
  6. getTextForNode(Node node)
  7. getTextForNode(Node node)
  8. getTextFromNode(Node node, String defaultValue)
  9. getTexto(Node n)