[jdom-interest] Finding XPath location for an Element

Rolf Lear jdom at tuis.net
Tue Jan 10 11:07:06 PST 2012


Hi Cliff

No method 'native' to JDOM, but the code is simple (and you can 'season'
to taste...):

String xpath = "";
Element p = element;
while (p != null) {
  xpath = "/" + p.getName() + xpath;
  p = p.getParentElement();
}
System.out.println(xpath);

But, the problem is that this will get *all* 'd' Elements that have an
ancestry with the same XPath

Rolf

On Tue, 10 Jan 2012 13:46:07 -0500, cliff palmer <palmercliff at gmail.com>
wrote:
> I'd like to be able to find the XPath search or the node hierarchy for
> an Element.  For example, if the Element is <d> in:
> <a>
>     <b>
>            <c>
>                  <d> </d>
>            </c>
>      </b>
> </a>
> 
> I'd like to have either the XPath search argument ("/a//b//c//d) or
> the list of nodes in the elements parents ("a b c d").
> 
> Is there a method that returns this?
> 
> Cliff
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com


More information about the jdom-interest mailing list