Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { public static Element findNode(Element parentNode, String nodeName) { if (parentNode == null) throw new NullPointerException("Parent Node cannot be null!"); if (nodeName == null) throw new NullPointerException("Nodename cannot be null!"); NodeList nodes = parentNode.getElementsByTagName(nodeName); if (nodes == null || nodes.getLength() == 0) return null; if (nodes.getLength() > 1) throw new RuntimeException(nodes.getLength() + " nodes found where only 1 was expected"); return (Element) nodes.item(0); } public static Element findNode(Document document, String nodeName) { if (document == null) throw new NullPointerException("Document cannot be null!"); if (nodeName == null) throw new NullPointerException("Nodename cannot be null!"); NodeList nodes = document.getElementsByTagName(nodeName); if (nodes == null || nodes.getLength() == 0) return null; if (nodes.getLength() > 1) throw new RuntimeException(nodes.getLength() + " nodes found where only 1 was expected"); return (Element) nodes.item(0); } }