Java tutorial
//package com.java2s; // are made available under the terms of the Eclipse Public License v1.0 import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static List<String> getChildTextsByTagName(Element element, String tagName) { List<Element> elements = getChildElementsByTagName(element, tagName); List<String> list = new ArrayList<String>(); for (Element e : elements) { String textValue = e.getTextContent().trim(); if (textValue.length() != 0) { list.add(textValue); } } return list; } /** * Gets the list of immediate child elements with the given tag name. * * @param element * @param tagName * @return list of {@link Element} objects */ public static List<Element> getChildElementsByTagName(Element element, String tagName) { List<Element> elements = new ArrayList<Element>(); NodeList list = element.getChildNodes(); int size = list.getLength(); if (size > 0) { for (int i = 0; i < size; i++) { Node node = list.item(i); if (node instanceof Element) { Element e = (Element) node; if (e.getTagName().equals(tagName)) { elements.add(e); } } } } return elements; } }