Java XML Element Get by Attribute findElementsByAttribute(Element node, String tagName, String attrName, List attrValues)

Here you can find the source of findElementsByAttribute(Element node, String tagName, String attrName, List attrValues)

Description

find Elements By Attribute

License

Open Source License

Declaration

public static List<Element> findElementsByAttribute(Element node, String tagName, String attrName,
            List<String> attrValues) 

Method Source Code

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

import java.util.ArrayList;

import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.Element;

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

public class Main {
    public static List<Element> findElementsByAttribute(Element node, String tagName, String attrName,
            List<String> attrValues) {
        List<Element> result = new LinkedList<Element>();
        NodeList nodeList = node.getChildNodes();
        if (nodeList == null) {
            return result;
        }/*from ww  w . j a  v a2s.  co  m*/
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Element element = checkIfElement(nodeList.item(i), tagName);
            if (element != null) {
                for (String value : attrValues) {
                    if (element.getAttribute(attrName).equals(value)) {
                        result.add(element);
                        break;
                    }
                }
            }
        }
        return result;
    }

    public static List<Element> findElementsByAttribute(Node node, String tagName, String attrName,
            String attrValue) {
        List<Element> result = new ArrayList<Element>();
        NodeList nodeList = node.getChildNodes();
        if (nodeList == null) {
            return result;
        }
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Element element = checkIfElement(nodeList.item(i), tagName);
            if (element != null && element.getAttribute(attrName).equals(attrValue)) {
                result.add(element);
            }
        }
        return result;
    }

    public static final Element checkIfElement(Node node) {
        Element result = null;
        if (isElement(node)) {
            result = (Element) node;
        }
        return result;
    }

    public static final Element checkIfElement(Node node, String tag) {
        Element result = null;
        if (isElement(node)) {
            Element tmp = (Element) node;
            if (tag == null || tmp.getTagName().equals(tag)) {
                result = tmp;
            }
        }
        return result;
    }

    public static boolean isElement(Node node) {
        return node.getNodeType() == Node.ELEMENT_NODE;
    }
}

Related

  1. findAllElementsByAttributes(Element node, String tagName, String attrName, List attrValues)
  2. findComponent(Element element, String attributeValue)
  3. findElement(Element element, String elementName, String attributeValue)
  4. findElement(Element parent, String elementNS, String elementName, String attrName, String attrValue)
  5. findElementByAttribute(final NodeList list, final String name, final String value)
  6. findElementWithAttributes(Node node, String tagName, Collection attrs)
  7. findElementWithNameAttribute(Element parent, String name)
  8. findElementWithUniqueAttribute(Element root, String elementName, String attribute, String attributeValue)
  9. findNode(Node node, String attr, String value)