[jdom-interest] Finding XPath location for an Element

Rolf Lear jdom at tuis.net
Tue Jan 10 12:14:05 PST 2012


Yes, you could do that and get an exact path to a particular element....

I had not thought it through as far as you, but the indexing is reasonable
(so is 'season to taste')....

But then you should probably also take it further again and ensure that
the namespace management is correct too....

but how would you set up the XPath references/links for an XPath query
with namespaces ... easily ... and in a 'general' way?

Rolf



On Tue, 10 Jan 2012 20:37:32 +0100, Paul Libbrecht <paul at hoplahup.net>
wrote:
> Isn't the trick to compute the index as in b[0] ??
> Lists of jdom using indexOf, elementA.getChildren('b').indexOf(elementB)
> are perfect for this.
> 
> paul
> 
> 
> Le 10 janv. 2012 à 20:07, Rolf Lear a écrit :
> 
>> 
>> 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
>> _______________________________________________
>> 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