Java XML Element Get getElementsByTagName(Element node, String tagName)

Here you can find the source of getElementsByTagName(Element node, String tagName)

Description

Search the children of the argument node for named elements.

License

LGPL

Parameter

Parameter Description
node The parent node element.
tagName The child element's tagName.

Return

The list of elements.

Declaration

public static ArrayList<Element> getElementsByTagName(Element node, String tagName) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        Web3d Consortium Copyright (c) 2007 - 2008
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 *****************************************************************************/

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*from  ww w  . j  av  a2 s.  c  om*/
     * Search the children of the argument node for named elements.
     *
     * @param node The parent node element.
     * @param tagName The child element's tagName.
     * @return The list of elements.
     */
    public static ArrayList<Element> getElementsByTagName(Element node, String tagName) {

        ArrayList<Element> list = new ArrayList<Element>();
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                if (e.getTagName().equals(tagName)) {
                    list.add(e);
                }
            }
        }
        return (list);
    }
}

Related

  1. getElementsByName(Element elem, String s)
  2. getElementsByTag(Element element, String tag)
  3. getElementsByTagName(Element aElement, String aTagName)
  4. getElementsByTagName(Element element, String tag)
  5. getElementsByTagName(Element element, String tagName)
  6. getElementsByTagName(Element node, String xmlns, String nodeName)
  7. getElementsByTagNameNS1(Element element, String nsName, String tagName)
  8. getElementsByTagNameNS1(Element element, String nsName, String tagName)
  9. getElementsByXpath(Document docInput, String xPath)