Java XML Attribute from Element getElementsByTagAndAttr(Element parent, String elemName, String attrName, String attrVal)

Here you can find the source of getElementsByTagAndAttr(Element parent, String elemName, String attrName, String attrVal)

Description

get Elements By Tag And Attr

License

LGPL

Declaration

public static List<Element> getElementsByTagAndAttr(Element parent, String elemName, String attrName,
            String attrVal) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

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 {
    public static List<Element> getElementsByTagAndAttr(Element parent, String elemName, String attrName,
            String attrVal) {//  ww  w .j a  v a2 s  .c  o  m
        List<Element> result = new ArrayList<Element>();
        NodeList nl = parent.getElementsByTagName(elemName);
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element elem = (Element) nl.item(i);
                String val = elem.getAttribute(attrName);
                if (val != null && val.equals(attrVal))
                    result.add(elem);
            }
        }
        return result;
    }
}

Related

  1. getElementBooleanValue(Element element, String attribute)
  2. getElementBooleanValue(Element element, String attribute)
  3. getElementBooleanValue(Element element, String attribute, boolean defaultValue)
  4. getElementByAttr(NodeList elements, String attrName)
  5. getElements(Element root, String tagName, String attrName)
  6. getElementStringValue(Element element, String attribute)
  7. getElementStringValue(Element element, String attribute)
  8. getElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue)
  9. getElementValues(final String elementName, final String attributeValue, final InputStream is)