commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oliver Heger <heg...@med.uni-marburg.de>
Subject Re: Commons Config : various XML config querying issues
Date Wed, 28 Sep 2005 06:04:17 GMT
Allistair Crossley schrieb:
> Hi,
> 
> Given an XML structure as follows;
> 
> <a>
>   <b x="1" y="2">
>     <c>
>       <d>1</role>
>       <d>2</role>
>       <d>3</role>
>     </c>
>     <e>
>       <f>1</f>
>       <f>2</f>
>     </e>
>   </b>
> 
>   <b x="3" y="4">
>     <c>
>       <d>1</role>
>       <d>2</role>
>       <d>3</role>
>     </c>
>     <e>
>       <f>1</f>
>       <f>2</f>
>     </e>
>   </b>
> </a>
> 
> I am having 2 issues.
> 
> Firstly, I cannot under any permutation whatsoever manage to get a non-null result for

> 
> Object o = config.getProperty("b");
> 
> or 
> 
> Object o = config.getProperty("a.b");
That's because getProperty() returns the text value of the specified 
key. None of the <b> tags has a direct text value, only attributes and 
sub elements.

> 
> However, I _can_ get a result when I know the index 
> 
> Object o = config.getProperty("b(0)[@x]");
> 
> In practice however, I do not know the index, and not being able to query just "b" means
I have no way of finding out how many indices there are.
If all of your <b> tags have the attribute x, you can do a

List xattrs = config.getList("b[@x]");

This will return the values of all x attributes of the b elements in the 
order of their occurence in the document. You can now iterate over this 
list to find a specific value. The index of this element can then be 
appended to the key "b" to obtain the data of further attributes or sub 
elements, e.g. if idx contains the (zero based) index:

config.getProperty("b(" + index + ")[@y]);

Alternatively you can also directly iterate over the configuration 
properties. This could look roughly like the following:

String value;
int idx = 0;

do
{
     String key = "b(" + idx + ")";
     value = config.getString(key + "[@x]");
     if(value != null)
     {
        // do something with other sub elements of b
        // e.g.
        // String b_y = config.getString(key + "[@y]");
     }
}while(value != null);

> 
> Furthermore, if I know the B I want to get the @y for, and I know this because I have
the b[@x], how do I express this? In xPath I would do something like
> 
> String prop = config.getProperty("b[@x='1'][@y]);
> 
> Other queries I would like to do would be to get a list of d's or a list of f's but only
within the context of a particular B which I can identify by b[@x] _not_ by an index which
means nothing.
> 
> Your advice appreciated, Allistair.
It is certainly true, that the query facilities of XMLConfiguration are 
somewhat limited. Support for XPath is an often requested feature and it 
is on our todo list, but not yet implemented. ATM you will have to 
determine the indices on your own and use them to construct keys for 
accessing the properties you need.

Oliver

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message