get XML Node Child By Attribute Name - Android XML

Android examples for XML:XML Attribute

Description

get XML Node Child By Attribute Name

Demo Code


//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Element getChildByAttrName(Node node, String attrName,
            String attrValue)//from  ww  w . j  a va  2s.c  o  m
    /*    */{
        /* 71 */NodeList nodeList = node.getChildNodes();
        /* 72 */int i = 0;
        for (int len = nodeList.getLength(); i < len; i++) {
            /* 73 */Node n = nodeList.item(i);
            /* 74 */if (n.getNodeType() == 1) {
                /* 75 */Element el = (Element) n;
                /* 76 */if (attrValue.equals(el.getAttribute(attrName))) {
                    /* 77 */return el;
                    /*    */}
                /*    */}
            /*    */}
        /*    */
        /* 82 */return null;
        /*    */}
}

Related Tutorials