In our application we've some static xml configuration for accessing routes. An example of an xml file looks like:
<configuration>
<level value="1" progress="1">
<route destination="a">ba</route>
</level>
<level value="1" progress="2">
<route destination="a">caba</route>
<route destination="b">cabb</route>
</level>
.. etc ..
</configuration>
On multiple occasion we need to retrieve the value(s) of the route, given arguments value
, progress
, and destination
(all optional, no arguments at all should return all routes).
I know how to achieve this with XPath, but I would like to use it in a spring bean, which can be wired into other spring beans, services.
I'm thinking something like
@Service
Class RouteConfiguration implements IRouteConfiguration {
Document xmlRoutes = null;
XPath xPath = XPathFactory.newInstance().newXPath();
// Constructor
public RouteConfiguration() {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
xmlRoutes = docBuilder.parse (this.getClass().getResourceAsStream(url));
} catch AndAll...
// method
public List<String> getRoutes(Integer level, Integer progress, String destination) {
String expression = String.format("/configuration/level[@value='%d' and @progress='%d']/route[@destination='%s']", level, progress, destination);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlRoutes, XPathConstants.NODESET);
// Of course some more plumbing needed to cater for optional arguments and to get result List.
}
I'm wondering is this the right approach? I know that Spring has XML support, but as far as I can see this applies for xml (webservice) messages. Also I'm worried about concurrency and possible performance issues? Would there be better solutions to tackle this (I can create an Object Graph out of the xsd and use jxpath, or plain java code to get through the final result)?