Here you can find the source of getChildElementsTextSet(Element parentElement, String tagName)
Parameter | Description |
---|---|
parentElement | Parent Element node |
tagName | child Element name |
public static Set<String> getChildElementsTextSet(Element parentElement, String tagName)
//package com.java2s; /*/*from w ww . j av a 2s . c om*/ * CDDL HEADER START * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at * http://www.sun.com/cddl/cddl.html and legal/CDDLv1.0.txt * See the License for the specific language governing * permission and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Copyright 2009 Sun Microsystems Inc. All Rights Reserved * CDDL HEADER END */ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Returns a Set of text content of all descendant Elements with a given tag name * Null is returned if there is no child element with given tag name * * @param parentElement Parent Element node * @param tagName child Element name * @return List<String> containing text content */ public static Set<String> getChildElementsTextSet(Element parentElement, String tagName) { List<Element> childElements = getChildElements(parentElement, tagName); if (childElements != null && childElements.size() > 0) { Set<String> textList = new HashSet<String>(childElements.size()); for (Element element : childElements) { textList.add(element.getTextContent().trim()); } return textList; } return Collections.EMPTY_SET; } protected static List<Element> getChildElements(Element element, String tagName) { List<Element> elements = getElementList(element); int numElements = elements.size(); List<Element> childElements = new ArrayList<Element>(numElements); for (int i = 0; i < numElements; i++) { Element childElement = (Element) elements.get(i); if (tagName != null) { String childTagName = childElement.getTagName(); if (!childTagName.equals(tagName)) { continue; } } childElements.add(childElement); } return childElements; } protected static List<Element> getElementList(Element element) { if (element == null) { return Collections.emptyList(); } NodeList childNodes = element.getChildNodes(); int numChildren = childNodes.getLength(); List<Element> elementList = new ArrayList<Element>(numChildren); for (int i = 0; i < numChildren; i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() != Node.ELEMENT_NODE) { continue; } elementList.add((Element) childNode); } return elementList; } }