Java XML First Child Element getFirstChildNode(Node parentNode, String childNodeName)

Here you can find the source of getFirstChildNode(Node parentNode, String childNodeName)

Description

Returns the first child node of parentNode with the given node name or null if no such child node exists.

License

Open Source License

Parameter

Parameter Description
parentNode a node
childNodeName node name of child node

Return

a node or null

Declaration

public static Node getFirstChildNode(Node parentNode, String childNodeName) 

Method Source Code

//package com.java2s;
/*/*from  w ww .j a va2s.  c o  m*/
 * The MIT License (MIT)
 * 
 * Copyright (c) 2010 Technische Universitaet Berlin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Vector;

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

public class Main {
    /**
     * Returns the first child node of <code>parentNode</code> with the given node name or
     * <code>null</code> if no such child node exists.
     * @param parentNode a node
     * @param childNodeName node name of child node
     * @return a node or <code>null</code>
     */
    public static Node getFirstChildNode(Node parentNode, String childNodeName) {
        NodeList nodes = parentNode.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            if (n.getNodeType() == Node.TEXT_NODE)
                continue;
            if (n.getNodeName().equals(childNodeName))
                return n;
        }
        return null;
    }

    /**
     * Returns an array of all child nodes of <code>parentNode</code> with the given node name or
     * an empty list if no such child nodes exist. If the node name is <code>null</code> every
     * non-text and non-comment node will be returned.
     * 
     * @param parentNode a node
     * @param childNodeName node name of child nodes, may be <code>null</code>
     * @return a list of nodes
     */
    public static Node[] getChildNodes(Node parentNode, String childNodeName) {
        Vector children = new Vector();
        NodeList nodes = parentNode.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.COMMENT_NODE)
                continue;
            if (childNodeName == null || n.getNodeName().equals(childNodeName))
                children.add(n);
        }
        Node[] result = new Node[children.size()];
        children.toArray(result);
        return result;
    }
}

Related

  1. getFirstChildElementWithName(Element elem, String name)
  2. getFirstChildElmtByTag(String aTagName)
  3. getFirstChildElmtByTagAndAttribut(Node aNode, String aTagName, String aAttrId, String aAttrValue)
  4. getFirstChildNamed(Element elem, String childName)
  5. getFirstChildNamed(Node node, String name)
  6. getFirstChildNodeWithName(String nodeName, Node parentNode)
  7. getFirstChildOfTagName(Element elem, String name)
  8. getFirstChildOfType(final Element elParent, final String childTag)
  9. getFirstChildTag(Element el, String tag)