Consider I have the following example XML file:
My recommendation is to use a SAX parser. wiki entry for SAX , Xerces: a SAX parser for java by Apache
On each start element, add the name of the element onto the end of a list. On each end element, remove the last list entry. When you run into content, and you want to output your xpath, it can be retrieved by iterating the list.