开发者

Configuration file with variable number of objects

would like to get some feedback on using XML config files when the number of parameter sets inside can change.

Currently I load in program parameters (contract specifications for trading programs) with an XML file using the Apache Commons Configurations, for example, in a try block in an object's constructor:

XMLConfiguration config = new XMLConfiguration("TraderParms.xml");
m_myContract.m_symbol=config.getString("contract.symbol");
m_myContract.m_expiry=config.getString("contract.expiry");

This is fine for have a fixed number of contracts (parameter sets), but for some applications I'd like to load any number of them. Practically we are talking about less than ten.

This would seem to require some way to iterate over a set of things and specify that set in the XML file.

Or, at this point, should I be using a database?

I find it a little more convenient to edit XML files directly, so if it is a question of slightly less elegant code with XML开发者_如何学运维, I'd rather do that.


From this howto guide it seems that you can have a list of strings in your XML:

<buttons>
  <name>OK,Cancel,Help</name>
</buttons>

And retrieve it using the getList() method:

List buttons = config.getList("buttons.name");

That same page also shows how to deal with a collection of parameters:

<database>
  <tables>
    <table tableType="system">
      <name>users</name>
    </table>
    <table tableType="application">
      <name>documents</name>
    </table>
  </tables>
</database>

which can be accessed this way:

List prop = (List)config.getProperty("tables.table.name"); // get list of strings
config.getProperty("tables.table(0).name") // access first table name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜