Java XML First Child Element getFirstChildElement(Node parent)

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

Description

Gets the first child element of a node.

License

Open Source License

Parameter

Parameter Description
parent the node

Return

the first child element or null if there are no child elements

Declaration

private static Element getFirstChildElement(Node parent) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*from w  ww  .ja  v  a 2  s.co  m*/
     * Gets the first child element of an element.
     * @param parent the parent element
     * @return the first child element or null if there are no child elements
     */
    public static Element getFirstChildElement(Element parent) {
        return getFirstChildElement((Node) parent);
    }

    /**
     * Gets the first child element of a node.
     * @param parent the node
     * @return the first child element or null if there are no child elements
     */
    private static Element getFirstChildElement(Node parent) {
        NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                return (Element) node;
            }
        }
        return null;
    }
}

Related

  1. getFirstChildElement(Node node)
  2. getFirstChildElement(Node node, String tag)
  3. getFirstChildElement(Node parent)
  4. getFirstChildElement(Node parent)
  5. getFirstChildElement(Node parent)
  6. getFirstChildElement(Node parent)
  7. getFirstChildElement(Node parent)
  8. getFirstChildElement(Node parent)
  9. getFirstChildElement(Node rootNode)