Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Get Element by according tag * Only one such element should exist * * @param document document to search * @param tag tag to look for in document * @return corresponding element */ public static Element getSingleAppearingElementByTag(Document document, String tag) { NodeList elementList = document.getElementsByTagName(tag); if (elementList.getLength() != 1) return null; Node nNode = elementList.item(0); if (nNode.getNodeType() == Node.ELEMENT_NODE) { return (Element) nNode; } return null; } }