Java XML First Child Element getFirstChildNamed(Node node, String name)

Here you can find the source of getFirstChildNamed(Node node, String name)

Description

Returns the first child of node that has the given name, or null.

License

Open Source License

Parameter

Parameter Description
node A node.
name The name of the node we are looking for.

Return

The first node whose name matches, or null.

Declaration

public static Node getFirstChildNamed(Node node, String name) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Node;

public class Main {
    /**/*from  w ww.j  av a 2  s  .c  om*/
     * Returns the first child of <code>node</code> that has the given name, or
     * <code>null</code>.
     * 
     * @param node
     *            A node.
     * @param name
     *            The name of the node we are looking for.
     * @return The first node whose name matches, or <code>null</code>.
     */
    public static Node getFirstChildNamed(Node node, String name) {
        return getFirstSiblingNamed(node.getFirstChild(), name);
    }

    /**
     * Returns the first sibling of <code>node</code>, or <code>node</code>
     * itself, which has the given name. If none is found, the function returns
     * <code>null</code>.
     * 
     * @param node
     *            A node.
     * @param name
     *            The name of the node we are looking for.
     * @return The first node whose name matches, or <code>null</code>.
     */
    public static Node getFirstSiblingNamed(Node node, String name) {
        while (node != null && !node.getNodeName().equals(name)) {
            node = node.getNextSibling();
        }

        return node;
    }
}

Related

  1. getFirstChildElementsByTagName(Node contextNode, String elementName)
  2. getFirstChildElementWithName(Element elem, String name)
  3. getFirstChildElmtByTag(String aTagName)
  4. getFirstChildElmtByTagAndAttribut(Node aNode, String aTagName, String aAttrId, String aAttrValue)
  5. getFirstChildNamed(Element elem, String childName)
  6. getFirstChildNode(Node parentNode, String childNodeName)
  7. getFirstChildNodeWithName(String nodeName, Node parentNode)
  8. getFirstChildOfTagName(Element elem, String name)
  9. getFirstChildOfType(final Element elParent, final String childTag)