Java tutorial
//package com.java2s; import org.w3c.dom.*; public class Main { public static Document doc = null; private static Node getSingleNodeElementByTagName(String tagName) throws Exception { NodeList list = getNodeList(tagName); Node node = null; if (list.getLength() > 0 && list.item(0).getNodeType() == Node.ELEMENT_NODE) { node = list.item(0); } else { throw new Exception("Xpath Query did not result in a Node element. Check your Xpath expression"); } return node; } private static Node getSingleNodeElementByTagName(String tagName, int indexZeroBased) throws Exception { NodeList list = getNodeList(tagName); Node node = null; if (list.getLength() > 0 && list.item(indexZeroBased).getNodeType() == Node.ELEMENT_NODE) { node = list.item(indexZeroBased); } else { throw new Exception("Xpath Query did not result in any Node elements. Check your Xpath expression"); } return node; } private static NodeList getNodeList(String tagName) throws Exception { NodeList nodeList = doc.getElementsByTagName(tagName); return nodeList; } }