Java XML Child Get by Name getChildElements(Element element, String tagName)

Here you can find the source of getChildElements(Element element, String tagName)

Description

get Child Elements

License

Open Source License

Declaration

public static List<Element> getChildElements(Element element, String tagName) 

Method Source Code


//package com.java2s;
/*//ww w. ja  va  2 s .  com
 * 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.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public 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;
    }

    private 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;
    }
}

Related

  1. getChildElement(String name, Element el)
  2. getChildElement(String name, Element elem)
  3. getChildElements(Element element, String childrenName)
  4. getChildElements(Element element, String namespace, String localName)
  5. getChildElements(Element element, String tag)
  6. getChildElements(Element p_rootElement, String p_elementName)
  7. getChildElements(Element parent, String elementTag)
  8. getChildElements(Element parent, String localName)
  9. getChildElements(Element parent, String name)