Java XML Node Get by Name getElementsByTagName(Node node, String tagname)

Here you can find the source of getElementsByTagName(Node node, String tagname)

Description

get Elements By Tag Name

License

Open Source License

Declaration

static public List<Element> getElementsByTagName(Node node, String tagname) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Document;

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

import org.w3c.dom.Element;

public class Main {
    static public List<Element> getElementsByTagName(Node node, String tagname) {
        ArrayList<Element> list = new ArrayList<Element>();
        NodeList nl;/*w  w  w. j  av  a2s .  c o  m*/
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
            nl = ((Document) node).getElementsByTagName(tagname);
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            nl = ((Element) node).getElementsByTagName(tagname);
        } else {
            return null;
        }
        for (int i = 0; i < nl.getLength(); i++) {
            list.add((Element) nl.item(i));
        }
        return list;
    }
}

Related

  1. getElementsByTagName(Node node, String tagName)
  2. getElementsByTagName(Node node, String tagName)
  3. getElementsByTagName(Node root, String tagName)
  4. getElementsByTagNameNS(Node element, String namespace, String name)