Java XML Child Get by Name getChildByName(Node node, String name)

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

Description

Returns the first child of a node with a specified name.

License

Creative Commons License

Parameter

Parameter Description
node The parent node.
name The name of the child.

Return

The first child of node named name.

Declaration

public static Node getChildByName(Node node, String name) 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

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

public class Main {
    /**// w ww . ja  v  a 2s.  com
     * Returns the first child of a node with a specified name.
     * 
     * @param node The parent node.
     * @param name The name of the child.
     * @return The first child of <code>node</code> named <code>name</code>.
     */
    public static Node getChildByName(Node node, String name) {
        try {
            NodeList list = node.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                if (list.item(i).getNodeName().equals(name)) {
                    return list.item(i);
                }
            }
            return null;

        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getChildByName(Element parent, String name)
  2. getChildByName(final Element parent, final String name)
  3. getChildByName(final Element parent, final String name)
  4. getChildByName(Node node, String name)
  5. getChildByName(Node node, String name)
  6. getChildByName(Node node, String nodeName)
  7. getChildByTagName(Element e, String name)
  8. getChildByTagName(Element element, String childElementName)
  9. getChildByTagName(Element element, String tag)