get XML Child With Attribute - Android XML

Android examples for XML:XML Attribute

Description

get XML Child With Attribute

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 getChildWithAttribute(Element parent,
            String attrName) {/*from   ww  w  . ja va 2  s  .  co m*/
        NodeList childNodes = parent.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;
                if (!"".equals(element.getAttribute(attrName))) {
                    return element;
                }
            }
        }
        return null;
    }
}

Related Tutorials