I'm using Apache Commons Configuration with XML for the configuration of my application, and I'm having trouble getting a property that I want.
I have the following XML structure (minimal structure just to get point across):
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<planets>
<planet name="mars">
<terrain>terrain/mars.terrain</terrain>
</planet>
<planet name="earth">
<terrain>terrain/earth.terrain</terrain>
</planet>
</planets>
</settings>
As I understand it, the best I can do here to get the mars terrain
setting is:
config.getProperty("planets.planet(0).terrain");
Unfortunately, my app doesn't/shouldn't have any concept of the indexes of these planets. At least, the indexes aren't going to be consistent. They could change at any time, so it's unreliable to refer to them by index as is done above.
So, what I want to be able to do is something like this:
config.getProperty("planets.planet[@name=mars].terrain");
That doesn't work, as you might have guessed. The only other way I've found to do it is obtuse and unacceptable:
List<Object> planets = config.getList("planets.planet[@name]");
String terrain = "";
for (int i = 0; i < planets.size(); i++) {
if (planets[i].toString().equals("mars")) {
terrain = config.getString("planets.planet(" + i + ").terrain");
break;
}
}
I'm sure you can see why that's undesirable. Now, I'm to the point where I'm considering just wrapping/extending the Apache Commons Configuration library in order to add this type of functionality, but I'm just unwilling to accept that there isn't an easier way to do this.
What am I overlooking, and how can this be accomplished in a simpler manner? Does this functionality simply not exist?
I found out that I could replace the DefaultExpressionEngine with an XPathExpressionEngine.
XMLConfiguration.setDefaultExpressionEngine(new XPathExpressionEngine());
This allowed me to use XPath to get my properties, and I could now do:
String terrainFile = config.getString("planets/planet[@name='mars']/terrain");
One thing to note is that you need the Apache Commons JXPath lib to use the XPathExpressionEngine, or you will get an error when you try to create it.