Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /***********************************************************************/ public static Node traverseToTag(String tag, Node node) { Node n = traverseToInnerTag(tag, node); if (n == null) { throw new NullPointerException(String.format("The Tag '%s' could not be found from '%s'", tag, node)); } return n; } /***********************************************************************/ private static Node traverseToInnerTag(String tag, Node node) { String name = node.getNodeName(); if (name.equals(tag)) { return node; } else { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = traverseToInnerTag(tag, childNodes.item(i)); if (child != null) { return child; } } } return null; } }