java实现Properties读取XML
package cn.bob.ad.property;
import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
public class XmlConfig {
private static Properties PROPERTIES;
private static String DEFAULTCONFIG = "/application.local.xml";
static {
PROPERTIES = new Properties();
try {
Class<?> config_class = Class.forName(XmlConfig.class.getName());
InputStream in = config_class.getResourceAsStream(DEFAULTCONFIG);
try {
PROPERTIES.loadFromXML(in);
} catch (InvalidPropertiesFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(PROPERTIES.getProperty("query.all.video"));
}
}
XML文件书写格式:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties version="1.0">
<entry key="url">url</entry>
<entry key="user">user</entry>
<entry key="password">password....</entry>
<entry key="driver">driver</entry>
<!-- 命令行 -->
<entry key="command">command....命令行...</entry>
<!-- 查询所有的视频 -->
<entry key="query.all.video">select * from video ?...</entry>
</properties>