public static void main(String[] args)
{
//找到配置文件的路径
String path = System.getProperty("user.dir") + "\\conf\\config.xml";
//ConfigurationManager 实例
ConfigurationManager cm = ConfigurationManager.getInstance();
//新建文件
File file = new File(path);
//XMLFIleHandler 类似于InputStreamHandler,setFile()需要set一个一个文件,而不是文件名。
XMLFileHandler xmlHeader = new XMLFileHandler();
xmlHeader.setFile(file);
try
{
cm.load(xmlHeader, "config");
}
catch (ConfigurationManagerException e)
{
e.printStackTrace();
}
//英文水平有限,官方说明如下
//A Configuraton provides access to all Category and Properties. The Configuration was converted into an Interface after the need for other Configuration implementations existed。
Configuration configuration = cm.getConfiguration("config");
//取配置文件general属性下所有节点的值。
//String[] arr = configuration.getPropertyNames("general");
/*System.out.println("arr.length=" + arr.length);
String key = null;
for(int i = 0; i < arr.length; i++)
{
key = arr[i];
System.out.println(configuration.getProperty(key));
}*/
String showNews = configuration.getProperty("showNews");
System.out.println(showNews);
}
附上config.xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<properties>
<variables>
<variable name="my.path" value="/home/foo/data" />
</variables>
<category name="general">
<property name="upload_dir" value="${my.path}/data" />
<property name="NewsCounter" value="10" />
<property name="showNews" value="true" />
<property name="MyProp" value="Hello world" />
</category>
<category name="JDBC">
<property name="URL" value="jdbc:mysql://localhost/iportal" />
<property name="DRIVER" value="org.gjt.mm.mysql.Driver" />
<property name="PWD" value="pwd" />
<property name="USER" value="user" />
</category>
</properties>
说明:Jconfig读xml的时候传参数 my.path 。
{
//找到配置文件的路径
String path = System.getProperty("user.dir") + "\\conf\\config.xml";
//ConfigurationManager 实例
ConfigurationManager cm = ConfigurationManager.getInstance();
//新建文件
File file = new File(path);
//XMLFIleHandler 类似于InputStreamHandler,setFile()需要set一个一个文件,而不是文件名。
XMLFileHandler xmlHeader = new XMLFileHandler();
xmlHeader.setFile(file);
try
{
cm.load(xmlHeader, "config");
}
catch (ConfigurationManagerException e)
{
e.printStackTrace();
}
//英文水平有限,官方说明如下
//A Configuraton provides access to all Category and Properties. The Configuration was converted into an Interface after the need for other Configuration implementations existed。
Configuration configuration = cm.getConfiguration("config");
//取配置文件general属性下所有节点的值。
//String[] arr = configuration.getPropertyNames("general");
/*System.out.println("arr.length=" + arr.length);
String key = null;
for(int i = 0; i < arr.length; i++)
{
key = arr[i];
System.out.println(configuration.getProperty(key));
}*/
String showNews = configuration.getProperty("showNews");
System.out.println(showNews);
}
附上config.xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<properties>
<variables>
<variable name="my.path" value="/home/foo/data" />
</variables>
<category name="general">
<property name="upload_dir" value="${my.path}/data" />
<property name="NewsCounter" value="10" />
<property name="showNews" value="true" />
<property name="MyProp" value="Hello world" />
</category>
<category name="JDBC">
<property name="URL" value="jdbc:mysql://localhost/iportal" />
<property name="DRIVER" value="org.gjt.mm.mysql.Driver" />
<property name="PWD" value="pwd" />
<property name="USER" value="user" />
</category>
</properties>
说明:Jconfig读xml的时候传参数 my.path 。