Java Utililty Methods XML Element Get by Attribute

List of utility methods to do XML Element Get by Attribute

Description

The list of methods to do XML Element Get by Attribute are organized into topic(s).

Method

voidgetElementsWithAttributeEquals(Element root, String attribute, String value, List list)
Retrieve the list of the elements which have an attribute equal to the given value (recursive function).
if (root.getAttribute(attribute).equals(value)) {
    list.add(root);
if (root.hasChildNodes()) {
    NodeList nodes = root.getChildNodes();
    int length = nodes.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nodes.item(i);
...
ElementgetElementWithAttribute(String tagName, String attribute, String attributeValue, Element searchIn)
get Element With Attribute
ArrayList<Element> list = getElementsByTag(tagName, searchIn);
for (Element e : list) {
    if (attributeValue.equals(e.getAttribute(attribute))) {
        return e;
return null;
ElementgetElementWithAttributeNS(Element element, String namespaceURI, String attribute, String value)
Get the first element descendant having the specified attribute name and value.
String attr = element.getAttributeNS(namespaceURI, attribute);
if (attr != null && attr.equals(value)) {
    return element;
NodeList childs = element.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
    Node node = childs.item(i);
    if (Node.ELEMENT_NODE == node.getNodeType()) {
...
ListselectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)
Return a list of named Elements with a specific attribute value.
NodeList elementList = element.getElementsByTagName(name);
List resultList = new ArrayList();
for (int i = 0; i < elementList.getLength(); i++) {
    if (getAttribute((Element) elementList.item(i), attribute).equals(value)) {
        resultList.add(elementList.item(i));
        if (returnFirst) {
            break;
return resultList;
ElementselectFirstElementByAttributeValueNS(String namespace, Element element, String name, String attribute, String value)
Return the first named Element with a specific attribute value (namespace aware) namespace The namespace URI
ArrayList resultList;
resultList = (ArrayList) selectElementsByAttributeValueNS(namespace, element, name, attribute, value, true);
return (resultList.size() == 0) ? null : (Element) resultList.get(0);