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

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

Description

Gets the list of immediate child elements with the given tag name.

License

Open Source License

Parameter

Parameter Description
element a parameter
tagName a parameter

Return

list of objects

Declaration

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

Method Source Code

//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 {
    /**/*from   w w w. j a v a  2 s .c o m*/
     * 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;
    }
}

Related

  1. getChildElementsByTagName(Element ele, String childEleName)
  2. getChildElementsByTagName(Element ele, String childEleName)
  3. getChildElementsByTagName(Element ele, String childEleName, boolean localName)
  4. getChildElementsByTagName(Element elem, String name)
  5. getChildElementsByTagName(Element element, String name)
  6. getChildElementsByTagName(Element parentElement, String childTag)
  7. getChildElementsByTagName(Element parentElement, String name)
  8. getChildElementsByTagName(Element parentNode, String tagName)
  9. getChildElementsByTagName(final Element element, final String tagName)