Java tutorial
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * An iterable for all the Element childs of a node * * @param n * the node get the children from * @return An iterable for all the Element childs of a node */ public static Iterable<Element> elements(Element n) { int sz = n.getChildNodes().getLength(); ArrayList<Element> elements = new ArrayList<Element>(sz); for (int idx = 0; idx < sz; idx++) { Node node = n.getChildNodes().item(idx); if (node instanceof Element) elements.add((Element) node); } return elements; } /** * An Iterable for the Element's childs, with a particular name, of a node * * @param n * the node get the children from * @param elementName * the name of the child elements * @return An Iterable for the Element's children, with a particular name, of a node */ public static Iterable<Element> elements(Element n, String elementName) { NodeList subNodes = n.getElementsByTagName(elementName); int sz = subNodes.getLength(); ArrayList<Element> elements = new ArrayList<Element>(sz); for (int idx = 0; idx < sz; idx++) { Node node = subNodes.item(idx); elements.add((Element) node); } return elements; } /** * An Iterable for the Element's childs, with a particular name, of a node * * @param n * the node get the children from * @param namespace * @param elementName * the name of the child elements * @return An Iterable for the Element's children, with a particular name, of a node */ public static Iterable<Element> elements(Element n, String namespace, String elementName) { NodeList subNodes = n.getElementsByTagNameNS(namespace, elementName); int sz = subNodes.getLength(); ArrayList<Element> elements = new ArrayList<Element>(sz); for (int idx = 0; idx < sz; idx++) { Node node = subNodes.item(idx); elements.add((Element) node); } return elements; } }