2

Lets say we have the following XML:

<root>
    <sub>
        <id>1</id>
        <values>
            <value>1</value>
            <value>2</value>
        </values>
    </sub>
    <sub>
        <id>2</id>
        <values>
            <value>16</value>
            <value>15</value> 
        </values>
    </sub>
</root>

What I want to with Apache Commons Configuration: I want to get all values for sub with id2. How could I achieve that? I find nothing in the documentation, how I can query with a dynamic hierarchy number.

Wayne
  • 59,728
  • 15
  • 131
  • 126
Dominik Obermaier
  • 5,610
  • 4
  • 34
  • 45

2 Answers2

4

You can use XPath:

//sub[id = 2]/values/value
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
1

// often leads to slow execution (causes the whole XML (sub) tree to be searched).

Use:

/*/sub[id = 2]/values/value

This selects any value element that is a child of a values element that is a child of a sub element whose id child has string value "2", and that (the sub) is a child of the top element of the XML document.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431