You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by M M <s....@gmail.com> on 2011/12/29 11:02:18 UTC

[configuration] XPathExpressionEngine - xpath syntax

  Hi, I'm trying to use /_*Commons Configuration*_/ to parse an XML 
configuration file and I'm having some troubles with the xpath syntax.
After a few tries I understood that I need to use the 
XPathExpressionEngine, and i think that the problems I'm facing are due 
to the xpath syntax I'm using.

Here is the xml file:
------------------------------------------------------------------------
<general-configuration>
<permissions>
<allowed-projects>
<project id="10020" key="TP1">test project 1</project>
<project id="10021" key="TP2">test project 2</project>
<project id="10022" key="TP3">test project 3</project>
</allowed-projects>

<allowed-groups>
<group id="xyz" name="test group"/>
</allowed-groups>

<allowed-users>
<user id="zxy" name="test user"/>
</allowed-users>
</permissions>

<kpi-definition>
<project id="10020" key="DP">
<issue-type id="1" name="Bug">
<kpi id="1" name="t0 - t1" days="0" hours="0" minutes="10">
<transition id="1" name="a">1</transition>
<transition id="2" name="b">2</transition>
</kpi>
<kpi id="2" name="t1 - t2" days="0" hours="1" minutes="0">
<transition id="3" name="c">1</transition>
<transition id="4" name="d">2</transition>
</kpi>
</issue-type>

<issue-type id="3" name="Task">

<kpi id="1" name="t0 - t1" days="0" hours="0" minutes="10">
<transition id="5" name="e"></transition>
<transition id="6" name="f"></transition>
</kpi>

<kpi id="2" name="t1 - t2" days="0" hours="1" minutes="0">
<transition id="7" name="g"></transition>
<transition id="8" name="h"></transition>
</kpi>
</issue-type>
</project>
</kpi-definition>
</general-configuration>
------------------------------------------------------------------------

And here is the code I wrote to try to understand how to get what i want 
using the XPathExpressionEngine
------------------------------------------------------------------------
private XMLConfiguration configurationFile;

configurationFile = new XMLConfiguration(/"kpi_configuration.xml"/);
configurationFile.setExpressionEngine(new XPathExpressionEngine());


DefaultExpressionEngine defaultEngine = new DefaultExpressionEngine();
defaultEngine.setPropertyDelimiter(/"/"/);
configurationFile.setExpressionEngine(defaultEngine);

*// using the default expression engine i get what i want, a list of the
// "id" attributes of the project tag (10020,10021,10022)*
List<String> testList001 = 
configurationFile.getList(/"permissions/allowed-projects/project[@id]"/);
for (Iterator<String> iterator = testList001.iterator(); 
iterator.hasNext();)
{
     System.out.println(iterator.next());
}

configurationFile.setExpressionEngine(new XPathExpressionEngine());
*// using the xpath expression engine, and giving exactly the same path 
to the getList method
// i get a completely different result! the list is made of the text 
content of the tag
// (test project 1, test project 2, test project 3)*
List<String> testList002 = 
configurationFile.getList(/"permissions/allowed-projects/project[@id]"/);
for (Iterator<String> iterator = testList002.iterator(); 
iterator.hasNext();)
{
     System.out.println(iterator.next());
}

*// here I tried to use the xpath syntax [@attribute=value] to get into 
a specific branch of the xml tree
// the expected result was at least a list of values, instead i get null 
object*
List<String> testList003 = 
configurationFile.getList(/"kpi-definition/project[@id=10020]/issue-type[@id=1]/transition"/);
for (Iterator<String> iterator = testList003.iterator(); 
iterator.hasNext();)
{
     System.out.println(iterator.next());
}

*// here I tried to use the xpath syntax [@attribute=value] to get into 
a specific branch of the xml tree
// the expected result was a single value instead i got a null object*
String testValue001 = 
configurationFile.getString(/"kpi-definition/project[@id=10020]/issue-type[@id=1]/transition[@name=a]"/);
System.out.println(testValue001);

Many, many thanks in advance to everyone who can help me!
I really can't work it out (... and I have to :) ... )
MM


Re: [configuration] XPathExpressionEngine - xpath syntax

Posted by M M <s....@gmail.com>.
Oliver, thank you very much!
The first hint is working perfectly.

Unfortunately the second change didn't solve the problem, I'm still 
getting null objects although I added the quotes...
I keep on doing tests, maybe I'll find the good syntax.

Thank you very much again!
Bye
MM



On 29/12/2011 21.27, Oliver Heger wrote:
> Hi, some comments below...
>
> Oliver
>
> Am 29.12.2011 11:02, schrieb M M:
>> Hi, I'm trying to use /_*Commons Configuration*_/ to parse an XML
>> configuration file and I'm having some troubles with the xpath syntax.
>> After a few tries I understood that I need to use the
>> XPathExpressionEngine, and i think that the problems I'm facing are due
>> to the xpath syntax I'm using.
>>
>> Here is the xml file:
>> ------------------------------------------------------------------------
>> <general-configuration>
>> <permissions>
>> <allowed-projects>
>> <project id="10020" key="TP1">test project 1</project>
>> <project id="10021" key="TP2">test project 2</project>
>> <project id="10022" key="TP3">test project 3</project>
>> </allowed-projects>
>>
>> <allowed-groups>
>> <group id="xyz" name="test group"/>
>> </allowed-groups>
>>
>> <allowed-users>
>> <user id="zxy" name="test user"/>
>> </allowed-users>
>> </permissions>
>>
>> <kpi-definition>
>> <project id="10020" key="DP">
>> <issue-type id="1" name="Bug">
>> <kpi id="1" name="t0 - t1" days="0" hours="0" minutes="10">
>> <transition id="1" name="a">1</transition>
>> <transition id="2" name="b">2</transition>
>> </kpi>
>> <kpi id="2" name="t1 - t2" days="0" hours="1" minutes="0">
>> <transition id="3" name="c">1</transition>
>> <transition id="4" name="d">2</transition>
>> </kpi>
>> </issue-type>
>>
>> <issue-type id="3" name="Task">
>>
>> <kpi id="1" name="t0 - t1" days="0" hours="0" minutes="10">
>> <transition id="5" name="e"></transition>
>> <transition id="6" name="f"></transition>
>> </kpi>
>>
>> <kpi id="2" name="t1 - t2" days="0" hours="1" minutes="0">
>> <transition id="7" name="g"></transition>
>> <transition id="8" name="h"></transition>
>> </kpi>
>> </issue-type>
>> </project>
>> </kpi-definition>
>> </general-configuration>
>> ------------------------------------------------------------------------
>>
>> And here is the code I wrote to try to understand how to get what i want
>> using the XPathExpressionEngine
>> ------------------------------------------------------------------------
>> private XMLConfiguration configurationFile;
>>
>> configurationFile = new XMLConfiguration(/"kpi_configuration.xml"/);
>> configurationFile.setExpressionEngine(new XPathExpressionEngine());
>>
>>
>> DefaultExpressionEngine defaultEngine = new DefaultExpressionEngine();
>> defaultEngine.setPropertyDelimiter(/"/"/);
>> configurationFile.setExpressionEngine(defaultEngine);
>>
>> *// using the default expression engine i get what i want, a list of the
>> // "id" attributes of the project tag (10020,10021,10022)*
>> List<String> testList001 =
>> configurationFile.getList(/"permissions/allowed-projects/project[@id]"/); 
>>
>> for (Iterator<String> iterator = testList001.iterator();
>> iterator.hasNext();)
>> {
>> System.out.println(iterator.next());
>> }
>>
>> configurationFile.setExpressionEngine(new XPathExpressionEngine());
>> *// using the xpath expression engine, and giving exactly the same path
>> to the getList method
>> // i get a completely different result! the list is made of the text
>> content of the tag
>> // (test project 1, test project 2, test project 3)*
>> List<String> testList002 =
>> configurationFile.getList(/"permissions/allowed-projects/project[@id]"/); 
>>
>> for (Iterator<String> iterator = testList002.iterator();
>> iterator.hasNext();)
>> {
>> System.out.println(iterator.next());
>> }
>
> Try the key "permissions/allowed-projects/project/@id"; the square 
> brackets are not needed for accessing attributes.
>
>>
>> *// here I tried to use the xpath syntax [@attribute=value] to get into
>> a specific branch of the xml tree
>> // the expected result was at least a list of values, instead i get null
>> object*
>> List<String> testList003 =
>> configurationFile.getList(/"kpi-definition/project[@id=10020]/issue-type[@id=1]/transition"/); 
>>
>>
>> for (Iterator<String> iterator = testList003.iterator();
>> iterator.hasNext();)
>> {
>> System.out.println(iterator.next());
>> }
>
> For this and the following example it may be necessary to quote the 
> values because they are strings, e.g.
> "kpi-definition/project[@id='10020']/issue-type[@id='1']/transition"
>
>>
>> *// here I tried to use the xpath syntax [@attribute=value] to get into
>> a specific branch of the xml tree
>> // the expected result was a single value instead i got a null object*
>> String testValue001 =
>> configurationFile.getString(/"kpi-definition/project[@id=10020]/issue-type[@id=1]/transition[@name=a]"/); 
>>
>>
>> System.out.println(testValue001);
>>
>> Many, many thanks in advance to everyone who can help me!
>> I really can't work it out (... and I have to :) ... )
>> MM
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

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


Re: [configuration] XPathExpressionEngine - xpath syntax

Posted by Oliver Heger <ol...@oliver-heger.de>.
Hi, some comments below...

Oliver

Am 29.12.2011 11:02, schrieb M M:
> Hi, I'm trying to use /_*Commons Configuration*_/ to parse an XML
> configuration file and I'm having some troubles with the xpath syntax.
> After a few tries I understood that I need to use the
> XPathExpressionEngine, and i think that the problems I'm facing are due
> to the xpath syntax I'm using.
>
> Here is the xml file:
> ------------------------------------------------------------------------
> <general-configuration>
> <permissions>
> <allowed-projects>
> <project id="10020" key="TP1">test project 1</project>
> <project id="10021" key="TP2">test project 2</project>
> <project id="10022" key="TP3">test project 3</project>
> </allowed-projects>
>
> <allowed-groups>
> <group id="xyz" name="test group"/>
> </allowed-groups>
>
> <allowed-users>
> <user id="zxy" name="test user"/>
> </allowed-users>
> </permissions>
>
> <kpi-definition>
> <project id="10020" key="DP">
> <issue-type id="1" name="Bug">
> <kpi id="1" name="t0 - t1" days="0" hours="0" minutes="10">
> <transition id="1" name="a">1</transition>
> <transition id="2" name="b">2</transition>
> </kpi>
> <kpi id="2" name="t1 - t2" days="0" hours="1" minutes="0">
> <transition id="3" name="c">1</transition>
> <transition id="4" name="d">2</transition>
> </kpi>
> </issue-type>
>
> <issue-type id="3" name="Task">
>
> <kpi id="1" name="t0 - t1" days="0" hours="0" minutes="10">
> <transition id="5" name="e"></transition>
> <transition id="6" name="f"></transition>
> </kpi>
>
> <kpi id="2" name="t1 - t2" days="0" hours="1" minutes="0">
> <transition id="7" name="g"></transition>
> <transition id="8" name="h"></transition>
> </kpi>
> </issue-type>
> </project>
> </kpi-definition>
> </general-configuration>
> ------------------------------------------------------------------------
>
> And here is the code I wrote to try to understand how to get what i want
> using the XPathExpressionEngine
> ------------------------------------------------------------------------
> private XMLConfiguration configurationFile;
>
> configurationFile = new XMLConfiguration(/"kpi_configuration.xml"/);
> configurationFile.setExpressionEngine(new XPathExpressionEngine());
>
>
> DefaultExpressionEngine defaultEngine = new DefaultExpressionEngine();
> defaultEngine.setPropertyDelimiter(/"/"/);
> configurationFile.setExpressionEngine(defaultEngine);
>
> *// using the default expression engine i get what i want, a list of the
> // "id" attributes of the project tag (10020,10021,10022)*
> List<String> testList001 =
> configurationFile.getList(/"permissions/allowed-projects/project[@id]"/);
> for (Iterator<String> iterator = testList001.iterator();
> iterator.hasNext();)
> {
> System.out.println(iterator.next());
> }
>
> configurationFile.setExpressionEngine(new XPathExpressionEngine());
> *// using the xpath expression engine, and giving exactly the same path
> to the getList method
> // i get a completely different result! the list is made of the text
> content of the tag
> // (test project 1, test project 2, test project 3)*
> List<String> testList002 =
> configurationFile.getList(/"permissions/allowed-projects/project[@id]"/);
> for (Iterator<String> iterator = testList002.iterator();
> iterator.hasNext();)
> {
> System.out.println(iterator.next());
> }

Try the key "permissions/allowed-projects/project/@id"; the square 
brackets are not needed for accessing attributes.

>
> *// here I tried to use the xpath syntax [@attribute=value] to get into
> a specific branch of the xml tree
> // the expected result was at least a list of values, instead i get null
> object*
> List<String> testList003 =
> configurationFile.getList(/"kpi-definition/project[@id=10020]/issue-type[@id=1]/transition"/);
>
> for (Iterator<String> iterator = testList003.iterator();
> iterator.hasNext();)
> {
> System.out.println(iterator.next());
> }

For this and the following example it may be necessary to quote the 
values because they are strings, e.g.
"kpi-definition/project[@id='10020']/issue-type[@id='1']/transition"

>
> *// here I tried to use the xpath syntax [@attribute=value] to get into
> a specific branch of the xml tree
> // the expected result was a single value instead i got a null object*
> String testValue001 =
> configurationFile.getString(/"kpi-definition/project[@id=10020]/issue-type[@id=1]/transition[@name=a]"/);
>
> System.out.println(testValue001);
>
> Many, many thanks in advance to everyone who can help me!
> I really can't work it out (... and I have to :) ... )
> MM
>
>


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