Java tutorial
import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Utils { /** * Get the first text node associated with this element * @param parent the node containing text * @return Text (trimmed of leanding/trailing whitespace, null if none) */ public static String getFirstText(Node parent) { return getTextNodeByNumber(parent, 1); } /** * Get the specified text node associated with this element * @param parent the node containing text * @param number The text node to fetch (1st, 2nd, etc) * @return Text (trimmed of leanding/trailing whitespace, null if none) */ public static String getTextNodeByNumber(Node parent, int number) { String text = null; int count = 1; if (parent != null) { for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if ((child.getNodeType() == Node.TEXT_NODE) && (count++ == number)) { text = child.getNodeValue(); return text.trim(); } } } return text; } }