get XML Child With Attribute Value - Java XML

Java examples for XML:XML Attribute

Description

get XML Child With Attribute Value

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 getChildWithAttributeValue(Element parent,
            String attrName, String attrValue) {
        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 (attrValue.equals(element.getAttribute(attrName))) {
                    return element;
                }// w  w w. j ava2  s . c o m
            }
        }
        return null;
    }
}

Related Tutorials