You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Allistair Crossley <Al...@QAS.com> on 2005/09/27 16:31:45 UTC

Commons Config : various XML config querying issues

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");

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.

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.


<FONT SIZE=1 FACE="VERDANA,ARIAL" COLOR=BLUE> 
-------------------------------------------------------
QAS Ltd.
Registered in England: No 2582055
Registered in Australia: No 082 851 474
-------------------------------------------------------
</FONT> <FONT SIZE=1 FACE="VERDANA,ARIAL" COLOR=BLACK> 
Disclaimer:  The information contained within this e-mail is confidential and may be privileged. This email is intended solely for the named recipient only; if you are not authorised you must not disclose, copy, distribute, or retain this message or any part of it. If you have received this message in error please contact the sender at once so that we may take the appropriate action and avoid troubling you further.  Any views expressed in this message are those of the individual sender.  QAS Limited has the right lawfully to record, monitor and inspect messages between its employees and any third party.  Your messages shall be subject to such lawful supervision as QAS Limited deems to be necessary in order to protect its information, its interests and its reputation.  

Whilst all efforts are made to safeguard Inbound and Outbound emails, QAS Limited cannot guarantee that attachments are virus free or compatible with your systems and does not accept any liability in respect of viruses or computer problems experienced.
</FONT>


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


Re: Commons Config : various XML config querying issues

Posted by Oliver Heger <he...@med.uni-marburg.de>.
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