Java XML Element Get getElementsByTagName(Element element, String tag)

Here you can find the source of getElementsByTagName(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 elements by tag name

Declaration

public static List<Element> getElementsByTagName(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.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*  w ww  .  jav  a 2 s.  co  m*/
     * get single element by tag name.
     *
     * @param element the element
     * @param tag the tag
     * @return the elements by tag name
     */
    public static List<Element> getElementsByTagName(Element element, String tag) {
        List<Element> elems = new ArrayList<Element>();
        NodeList list = element.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element e = (Element) node;
                if (e.getTagName().equals(tag)) {
                    elems.add(e);
                }
            }
        }
        return elems;
    }
}

Related

  1. getElements(Element topElm)
  2. getElements(final String elementName, final InputStream is, final boolean onlyValues)
  3. getElementsByName(Element elem, String s)
  4. getElementsByTag(Element element, String tag)
  5. getElementsByTagName(Element aElement, String aTagName)
  6. getElementsByTagName(Element element, String tagName)
  7. getElementsByTagName(Element node, String tagName)
  8. getElementsByTagName(Element node, String xmlns, String nodeName)
  9. getElementsByTagNameNS1(Element element, String nsName, String tagName)