日期:2014-05-16 浏览次数:21020 次
<dependencies> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>commons-jxpath</groupId> <artifactId>commons-jxpath</artifactId> <version>1.3</version> </dependency> </dependencies>
<?xmlversion="1.0"encoding="UTF-8"?> <!-- const.xml --> <config> <database> <url>127.0.0.1</url> <port>1521</port> <login>admin</login> <password>pass</password> </database> </config>
XMLConfiguration config =new XMLConfiguration("const.xml"); // 127.0.0.1 config.getString("database.url"); // 1521 config.getString("database.port");
getBoolean getByte getDouble getFloat getInt getInteger getList getLong getStringArray
<?xmlversion="1.0"encoding="UTF-8"?> <!-- const.xml --> <config> <databases> <database> <name>dev</name> <url>127.0.0.1</url> <port>1521</port> <login>admin</login> <password>pass</password> </database> <database> <name>production</name> <url>192.23.44.100</url> <port>1521</port> <login>admin</login> <password>not-so-easy-pass</password> </database> </databases>
XMLConfiguration config =new XMLConfiguration("const.xml"); // 127.0.0.1 config.getString("databases.database(0).url"); // 192.23.44.100 config.getString("databases.database(1).url");
XMLConfiguration config =new XMLConfiguration("const.xml"); config.setExpressionEngine(new XPathExpressionEngine());// 127.0.0.1 config.getString("databases/database[name = 'dev']/url"); // 192.23.44.100 config.getString("databases/database[name = 'production']/url");