find XML Child Element With Attribute - Android XML

Android examples for XML:XML Attribute

Description

find XML Child Element With Attribute

Demo Code


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

public class Main {
    public static Element findChildElementWithAttribute(Element parent,
            String name, String attribute, String value) {
        if (parent == null) {
            return null;
        }//from   ww w.  j  a v  a  2s  .  com
        org.w3c.dom.Node ret = parent.getFirstChild();
        while (ret != null
                && (!(ret instanceof Element)
                        || !ret.getNodeName().equals(name)
                        || ((Element) ret).getAttribute(attribute) == null || !((Element) ret)
                        .getAttribute(attribute).equals(value))) {
            ret = ret.getNextSibling();
        }
        return (Element) ret;
    }
}

Related Tutorials