Here you can find the source of getFirstMatchingChildNode(Node node, String nodeName)
static Node getFirstMatchingChildNode(Node node, String nodeName)
//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 { static Node getFirstMatchingChildNode(Node node, String nodeName) { return getFirstMatchingChildNode(node, nodeName, null, null); }//from w w w . j av a 2s .c o m static Node getFirstMatchingChildNode(Node node, String nodeName, String attributeName, List<String> attributeValues) { if (node == null || nodeName == null) { return null; } List<Node> nodes = getMatchingChildNodes(node, nodeName, attributeName, attributeValues); return (nodes == null || nodes.isEmpty()) ? null : (Node) nodes .get(0); } static List<Node> getMatchingChildNodes(Node node, String nodeName, String attributeName, List<String> attributeValues) { if (node == null || nodeName == null) { return null; } List<Node> nodes = new ArrayList(); NodeList nodeList = node.getChildNodes(); int i = 0; while (i < nodeList.getLength()) { Node childNode = nodeList.item(i); if (childNode.getNodeName().equals(nodeName) && nodeMatchesAttributeFilter(childNode, attributeName, attributeValues)) { nodes.add(childNode); } i++; } return nodes; } static boolean nodeMatchesAttributeFilter(Node node, String attributeName, List<String> attributeValues) { if (attributeName == null || attributeValues == null) { return true; } NamedNodeMap attrMap = node.getAttributes(); if (attrMap != null) { Node attrNode = attrMap.getNamedItem(attributeName); if (attrNode != null && attributeValues.contains(attrNode.getNodeValue())) { return true; } } return false; } static String getNodeValue(Node node) { return (node == null || node.getFirstChild() == null || node .getFirstChild().getNodeValue() == null) ? null : node .getFirstChild().getNodeValue().trim(); } }