简易读取配置在xml中的内容
一般我们会把经常修改的内容写在xml中利于我们修改,维护信息
使用co信息mmons-configuration.jar读取个人认为很方便ConfigXML
--------------------------------第一种--------------------------------------
ConfigXml类代码:
private static final String CONFIG_PATH = "config.xml";
???
??? private static Configuration config ;
???
??? static{
??????? try {
??????????? config = new XMLConfiguration(CONFIG_PATH);
??????? } catch (ConfigurationException e) {
??????????? e.printStackTrace();
??????? }
??? }
??? public static String getString(String infoLabel) {
??????? return config.getString(infoLabel);
??? }
???
??? public static boolean getBoolean(String infoLabel) {
??????? return config.getBoolean(infoLabel);
??? }
???
??? public static int getInt(String infoLabel) {
??????? return config.getInt(infoLabel);
??? }
???
??? public static double getDouble(String infoLabel) {
??????? return config.getDouble(infoLabel);
??? }
???
??? public static String[] getStringArray(String infoLabel) {
??????? return config.getStringArray(infoLabel);
??? }
??? public static void main(String[] args){
??????? System.out.println(getInt("cache.timeout"));
??? }
使用方法:
假如config.xml中有段内容为:
<students>
??? <sname>张三</sname>
</students>
读取方法只需要:ConfigXml.getString("students.sname");
-----------------------第二种方式----------------------------
/**
?* 读取配置文件内容
?* @author arron.huang
?* @date
?* ? 2011-06-20 14:12:00
?*/
public final class Context {
?
?private static Configuration config = ConfigUtil.getConfig("configuration.ini");
?
?public static Configuration getConfig(){
??return config;
?}
?
}
confirguration.ini文件内容:
sname=张三
?
读取方式:
String sname = Context.getConfig().getString("sname");
结果:sname = 张三
?
?