Java XML Child Node Get getChildNode(Node parent, String childName)

Here you can find the source of getChildNode(Node parent, String childName)

Description

Gets a named child node.

License

Apache License

Parameter

Parameter Description
parent Parent node
childName Name of the child node or null for the first child

Return

The child node or null if no such node exists

Declaration

public static Node getChildNode(Node parent, String childName) 

Method Source Code

//package com.java2s;
/*/*w ww . j  a  v  a  2  s .  c om*/
 *   Copyright 2007 skynamics AG
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * Gets a named child node.
     * In contrast to the Element.getElementsByTagName method, this method is fine
     * if there is only a single element by that name.<br>
     * Note: Only child \belements\b will be considered.
     *
     * @deprecated {@link #getChildElement}
     * @param parent Parent node
     * @param childName Name of the child node or null for the first child
     * @return The child node or null if no such node exists
     */
    public static Node getChildNode(Node parent, String childName) {
        return getChildElement(parent, childName);
    }

    /**
     * Gets a named child node by the node type.
     * In contrast to the Element.getElementsByTagName
     * method, this method is fine if there is only a single element by that name.\n Note: Only
     * child node of the given node type will be considered.
     *
     * @param parent Parent node
     * @param childName Name of the child node or null for the first child
     * @param nodeType see types of interface Node
     * @return The child node or null if no such node exists
     */
    public static Node getChildNode(Node parent, String childName, short nodeType) {
        // Search the child nodes for the correct one
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child.getNodeType() != nodeType)
                continue;

            if (childName == null || childName.equals(child.getNodeName())) {
                return child;
            }
        }

        return null;
    }

    /**
     * Gets a named child node.
     * In contrast to the Element.getElementsByTagName method, this method is fine
     * if there is only a single element by that name.<br>
     * Note: Only child \belements\b will be considered.
     *
     * @param parent Parent node
     * @param childName Name of the child node or null for the first child
     * @return The child node or null if no such node exists
     */
    public static Element getChildElement(Node parent, String childName) {
        return (Element) getChildNode(parent, childName, Node.ELEMENT_NODE);
    }
}

Related

  1. getChildNode(Node node, String name)
  2. getChildNode(Node node, String name)
  3. getChildNode(Node node, String name)
  4. getChildNode(Node node, String nodeName)
  5. getChildNode(Node node, String tag)
  6. getChildNode(Node parent, String tagName)
  7. getChildNode(Node parentNode, String childElementName)
  8. getChildNode(Node root, String childName)
  9. getChildNode(Node rootNode, String childName)