2012年4月5日 星期四

[Java] org.apache.commons.configuration Example

Link: http://commons.apache.org/configuration/

XML Configuration: (notification.xml)
<?xml version="1.0" encoding="UTF-8"?>

<config>
    <notification>
        <server>Server</server>
    </notification>
</config>

Code:
import org.apache.commons.configuration.XMLConfiguration;

try {
    XMLConfiguration config = new XMLConfiguration(getClass().getResource("notification.xml"));
    String server = config.getString("notification.server");
    System.out.println(server);
}
catch(Exception e) {
    e.printStackTrace();
}

Key:
  • getClass().getResource() to get config file
  • To get config value, use notification.server, not config.notification.server

沒有留言:

張貼留言