Java XML First Child Element getFirstNamedChild(Node n, String name)

Here you can find the source of getFirstNamedChild(Node n, String name)

Description

Check if the given node has a child with the specified name Return the child of the specified name, or null if not found

License

Open Source License

Parameter

Parameter Description
n The parent node
namespace The namespace name
name The node name

Declaration

public static Node getFirstNamedChild(Node n, String name) 

Method Source Code

//package com.java2s;
/*/*from  www . ja  va  2 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 {
    /**
     * Check if the given node has a child with the specified name
     * Return the child of the specified name, or null if not found
     * @param n The parent node
     * @param namespace The namespace name
     * @param name The node name
     * @return
     */
    public static Node getFirstNamedChild(Node n, String name) {
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node currentChild = children.item(i);
            String localName = currentChild.getNodeName();
            if (localName != null && localName.equalsIgnoreCase(name)) {
                return currentChild;
            }
        }
        return null;
    }
}

Related

  1. getFirstLevelChildElements(Element parent)
  2. getFirstLevelChildElementsByTagName(Element parent, String elementName)
  3. getFirstLevelChildElementsByTagName(Element parent, String elementName)
  4. getFirstMatchedValueByChildTagName(Node parent, String name)
  5. getFirstMatchingDeepChildByTagName(final Element e, final String tagName)
  6. getFirstNamedChild(Node node, String name)
  7. getFirstNamedChildNode(Element element, String string)
  8. getFirstNamedChildNode(Node root, String nodeName)
  9. getFirstNontextChild(Node d)