Java tutorial
//package com.java2s; import java.util.LinkedList; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Returns a list of the direct {@link Element} children of the given element. * * @param parent * The {@link Element} to get the children from. * @return A {@link LinkedList} of the children {@link Element}s. */ public static LinkedList<Element> getDirectChildren(Element parent) { LinkedList<Element> list = new LinkedList<>(); for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof Element) { list.add((Element) child); } } return list; } /** * Returns a list of the direct {@link Element} children of the given element whose names match * {@code tag}. * * @param parent * The {@link Element} to get the children from. * @param tag * The element name of the children to look for. * @return A {@link LinkedList} of the children {@link Element}s. */ public static LinkedList<Element> getDirectChildren(Element parent, String tag) { LinkedList<Element> list = new LinkedList<>(); for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof Element && tag.equals(child.getNodeName())) { list.add((Element) child); } } return list; } }