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

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

Description

get single element by tag name.

License

Open Source License

Parameter

Parameter Description
element the element
tag the tag

Return

the child elements

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**/*w w  w . j  a  va 2s  .c  o m*/
     * get single element by tag name.
     *
     * @param element the element
     * @return the child elements
     */
    public static List<Element> getChildElements(Element element) {
        return toElements(element.getChildNodes());
    }

    /**
     * get single element by tag name.
     *
     * @param element the element
     * @param tag the tag
     * @return the child elements
     */
    public static List<Element> getChildElements(Element element, String tag) {
        return toElements(element.getChildNodes(), tag);
    }

    /**
     * To elements.
     *
     * @param nodes the nodes
     * @return the list
     */
    public static List<Element> toElements(NodeList nodes) {
        return toElements(nodes, null);
    }

    /**
     * To elements.
     *
     * @param nodes the nodes
     * @param filter the filter
     * @return the list
     */
    private static List<Element> toElements(NodeList nodes, String filter) {
        List<Element> list = new ArrayList<Element>();
        for (int i = 0; i < nodes.getLength(); i++)
            if (nodes.item(i) instanceof Element) {
                Element e = (Element) nodes.item(i);
                if (filter == null || e.getTagName().equals(filter)) {
                    list.add(e);
                }
            }
        return list;
    }
}

Related

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