Java tutorial
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /*************************************************************************** * Searches nodes in the given list for a child with a given name and value. * * @param list * @param childName * @param value * @return * @throws Exception **************************************************************************/ public static Node searchNodes(NodeList list, String childName, String value) throws Exception { for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (value.equals(getChildValueByName((Element) node, childName))) return node; } return null; } /*************************************************************************** * Returns the value of the first child under the give element with the * given name. * * @param e * @param name * @return * @throws Exception **************************************************************************/ public static String getChildValueByName(Element e, String name) throws Exception { String s = "Not found"; /* * The getElementsByTagName() function returns ANY children under the * given element with the given tag name. This function is intended to * return the value of only an immediate child with a given name. */ NodeList childNodes = e.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (node.getNodeName().equals(name)) { if (node.getFirstChild() != null) s = node.getFirstChild().getNodeValue(); else s = ""; break; } } return s; } }