Android XML Node List Search findFirstNodeByAttribute(NodeList list, String attrName, String attrValue)

Here you can find the source of findFirstNodeByAttribute(NodeList list, String attrName, String attrValue)

Description

find First Node By Attribute

Declaration

public static Node findFirstNodeByAttribute(NodeList list,
            String attrName, String attrValue) 

Method Source Code

//package com.java2s;

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

public class Main {
    public static Node findFirstNodeByAttribute(NodeList list,
            String attrName, String attrValue) {
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            String nodeAttrValue = getElementAttr(node, attrName);
            if (nodeAttrValue != null && attrValue.equals(nodeAttrValue)) {
                return node;
            }//from   w  ww.  j  a  v a2s  .  c o  m
        }

        return null;
    }

    public static String getElementAttr(Node element, String attrName) {
        if (element.getAttributes() != null) {
            for (int i = 0; i < element.getAttributes().getLength(); i++) {
                Node child = element.getAttributes().item(i);
                if (child.getNodeName().equals(attrName)) {
                    return child.getNodeValue();
                }
            }
        }

        return null;
    }
}