Java tutorial
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); you may not import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.util.ArrayList; import java.util.List; public class Main { /** Returns all the direct child elements of the given element. */ public static List<Element> getElements(Element element) { List<Element> elements = new ArrayList<>(); for (Node node : toIterable(element.getChildNodes())) { if (node.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) node); } } return elements; } /** Converts a NodeList to an Iterable of Nodes. */ public static Iterable<Node> toIterable(NodeList nodeList) { List<Node> nodes = new ArrayList<>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { nodes.add(nodeList.item(i)); } return nodes; } }