Apache Common Configuration--超方便的配置文件读取解析

Intro

 

Commons Configuration provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources. Commons Configuration provides typed access to single, and multi-valued configuration parameters as demonstrated by the following code:

Double double = config.getDouble("number");
Integer integer = config.getInteger("number");

 

Configuration parameters may be loaded from the following sources:

  • Properties files
  • XML documents
  • Windows INI files
  • Property list files (plist)
  • JNDI
  • JDBC Datasource
  • System properties
  • Applet parameters
  • Servlet parameters

 

Different configuration sources can be mixed using a ConfigurationFactory and a CompositeConfiguration. Additional sources of configuration parameters can be created by using custom configuration objects. This customization can be achieved by extending AbstractConfiguration orAbstractFileConfiguration.

 

Dependencies

 

用例: 

  1. XMLConfiguration xmlConfig = new XMLConfiguration();  
  2. xmlConfig.setDelimiterParsingDisabled(true);  
  3. xmlConfig.setURL(TestJXPath.class.getResource("test.xml"));  
  4. xmlConfig.setExpressionEngine(new XPathExpressionEngine());  
  5. try {  
  6.     xmlConfig.load();  
  7. catch (ConfigurationException e) {  
  8.     // TODO Auto-generated catch block   
  9.     e.printStackTrace();  
  10. }  
  11. System.out.println(xmlConfig.getString("/company[@id = '101']/address/street"));  
  12. System.out.println(xmlConfig.getString("/company[@id = '101']/address/street[@id='id']"));  
 

test.xml参照http://blog.csdn.net/buyaore_wo/archive/2011/02/16/6188487.aspx中test.xml

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值