Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * 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;
    }
}