Java tutorial
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Gets the value from the first descendant of a given ancestor, where the * descendant has the specified name. * * @param ancestor * the ancestor element * @param tagname * the tag name of the descendant to find. * @return the value of the descendant or <code>null</code> if no descendant * with that name could be found or the descendant found has no * value. */ public static String getValueFromDescendant(Element ancestor, String tagname) { if (ancestor != null) { NodeList nl = ancestor.getElementsByTagName(tagname); if (!isEmpty(nl)) { Element e = (Element) nl.item(0); Node c = e.getFirstChild(); if (c != null) { return c.getNodeValue(); } } } return null; } /** * Gets the value from the first descendant of a given ancestor, where the * descendant has the specified name. * * @param ancestor * the ancestor element * @param tagname * the tag name of the descendant to find. * @param defaultString * a default string to return if the value found is * <code>null</code>. * @return the value of the descendant or <code>null</code> if no descendant * with that name could be found or the descendant found has no * value. */ public static String getValueFromDescendant(Element ancestor, String tagname, String defaultString) { String val = getValueFromDescendant(ancestor, tagname); return (val != null) ? val : defaultString; } /** * Null-safe check if a nodelist is empty. * * @param nodeList * the nodelist to check. * @return <code>true</code> if the given nodelist is <code>null</code> or * empty. */ public static boolean isEmpty(NodeList nodeList) { return nodeList == null || nodeList.getLength() == 0; } }