Java XML First Child Element getFirstChildElement(Node node)

Here you can find the source of getFirstChildElement(Node node)

Description

Gets the first child org.w3c.dom.Element of the given Node.

License

Open Source License

Parameter

Parameter Description
node the <code>Node</code> to search

Return

the first child Element of the given Node

Declaration

public static Element getFirstChildElement(Node node) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**// ww  w  .  j a  v  a2  s.c  o  m
     * Gets the first child {@link org.w3c.dom.Element} of the given <code>Node</code>.
     * 
     * @param node the <code>Node</code> to search
     * @return the first child <code>Element</code> of the given <code>Node</code>
     */
    public static Element getFirstChildElement(Node node) {
        NodeList children = null;
        int numChildren = 0;
        if (node == null || (children = node.getChildNodes()) == null
                || (numChildren = node.getChildNodes().getLength()) == 0) {
            return null;
        }
        Element result = null;
        for (int i = 0; i < numChildren && result == null; i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                result = (Element) child;
            }
        }
        return result;
    }
}

Related

  1. getFirstChildElement(Element root)
  2. getFirstChildElement(final Node node)
  3. getFirstChildElement(final Node parent, final String elemName)
  4. getFirstChildElement(Node n)
  5. getFirstChildElement(Node n, String ns, String localName)
  6. getFirstChildElement(Node node)
  7. getFirstChildElement(Node node)
  8. getFirstChildElement(Node node)
  9. getFirstChildElement(Node node)