Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.List; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static Node getFirstMatchingChildNode(Node node, String str) { return getFirstMatchingChildNode(node, str, null, null); } public static Node getFirstMatchingChildNode(Node node, String str, String str2, List<String> list) { if (node == null || str == null) { return null; } List matchingChildNodes = getMatchingChildNodes(node, str, str2, list); return (matchingChildNodes == null || matchingChildNodes.isEmpty()) ? null : (Node) matchingChildNodes.get(0); } public static List<Node> getMatchingChildNodes(Node node, String str) { return getMatchingChildNodes(node, str, null, null); } public static List<Node> getMatchingChildNodes(Node node, String str, String str2, List<String> list) { if (node == null || str == null) { return null; } List<Node> arrayList = new ArrayList(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); if (item.getNodeName().equals(str) && nodeMatchesAttributeFilter(item, str2, list)) { arrayList.add(item); } } return arrayList; } public static boolean nodeMatchesAttributeFilter(Node node, String str, List<String> list) { if (str == null || list == null) { return true; } NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { Node namedItem = attributes.getNamedItem(str); if (namedItem != null && list.contains(namedItem.getNodeValue())) { return true; } } return false; } public static String getNodeValue(Node node) { return (node == null || node.getFirstChild() == null || node.getFirstChild().getNodeValue() == null) ? null : node.getFirstChild().getNodeValue().trim(); } }