Java XML Element Get by Attribute getElementWithAttribute(String tagName, String attribute, String attributeValue, Element searchIn)

Here you can find the source of getElementWithAttribute(String tagName, String attribute, String attributeValue, Element searchIn)

Description

get Element With Attribute

License

Open Source License

Declaration

public static Element getElementWithAttribute(String tagName, String attribute, String attributeValue,
        Element searchIn) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

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

public class Main {
    /**/*from w w  w  .  java  2  s.c  o  m*/
     */
    public static Element getElementWithAttribute(String tagName, String attribute, String attributeValue,
            Element searchIn) {
        ArrayList<Element> list = getElementsByTag(tagName, searchIn);
        for (Element e : list) {
            if (attributeValue.equals(e.getAttribute(attribute))) {
                return e;
            }
        }
        return null;
    }

    /**
     */
    public static ArrayList<Element> getElementsByTag(String tag, Element searchIn) {
        NodeList list = searchIn.getElementsByTagName(tag);
        ArrayList<Element> toReturn = new ArrayList<Element>();
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            if (!(n instanceof Element)) {
                continue;
            }
            toReturn.add((Element) n);
        }
        return toReturn;
    }
}

Related

  1. getElementIntAttribute(Element e, String whichAttribute)
  2. getElementsWithAttribute(Element element, String attribute)
  3. getElementsWithAttribute(Element element, String namespaceURI, String localName, String value, Collection elements)
  4. getElementsWithAttributeEquals( Element root, String attribute, String value)
  5. getElementsWithAttributeEquals(Element root, String attribute, String value, List list)
  6. getElementWithAttributeNS(Element element, String namespaceURI, String attribute, String value)
  7. selectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)
  8. selectFirstElementByAttributeValueNS( String namespace, Element element, String name, String attribute, String value)