Java XML First Child Element getFirstTextChild(Node n)

Here you can find the source of getFirstTextChild(Node n)

Description

Return the first text child node of a given node Returns null if there is no text child node

License

Open Source License

Parameter

Parameter Description
n a parameter

Declaration

public static Node getFirstTextChild(Node n) 

Method Source Code

//package com.java2s;
/*// w  w  w . ja va2  s. c om
 This file is part of p300.
    
    
 p300 is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 p300 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 General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with p300.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Return the first text child node of a given node
     * Returns null if there is no text child node
     * @param n
     * @return
     */
    public static Node getFirstTextChild(Node n) {
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node currentChild = children.item(i);
            if (children.item(i).getNodeType() == Node.TEXT_NODE) {
                return currentChild;
            }
        }
        return null;
    }

    /**
     * Return the first text child of a node that contains the given text
     * Returns null if no such child exists
     * @param n
     * @param text
     * @return
     */
    public static Node getFirstTextChild(Node n, String text) {
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node currentChild = children.item(i);
            String nodeValue = currentChild.getNodeValue();
            if (nodeValue != null && nodeValue.equalsIgnoreCase(text)) {
                return currentChild;
            }
        }
        return null;
    }
}

Related

  1. getFirstNamedChild(Node node, String name)
  2. getFirstNamedChildNode(Element element, String string)
  3. getFirstNamedChildNode(Node root, String nodeName)
  4. getFirstNontextChild(Node d)
  5. getFirstNonTextChild(Node root)
  6. getFirstTextChild(Node node)
  7. getFirstVisibleChildElement(Node parent)
  8. getValueOfFirstElementChild(final Element element, final String namespace, final String localname)
  9. insertBeforeFirstChild(Element object, Element attrId)