get Item By XML Child Text Value - Java XML

Java examples for XML:XML Element Value

Description

get Item By XML Child Text Value

Demo Code


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

public class Main {
    public static Node getItemByChildTextValue(NodeList nodes,
            String childTag, String value) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node item = nodes.item(i);
            if (getChild(item, childTag).getTextContent().equals(value))
                return item;
        }/* w  ww. j a v  a 2  s.  co m*/
        return null;
    }

    public static Node getChild(Node node, String tag) {
        NodeList nodes = node.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node item = nodes.item(i);
            if (item.getNodeName().equals(tag))
                return item;
        }
        return null;
    }
}

Related Tutorials