首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

java兑现Properties读取XML

2012-11-09 
java实现Properties读取XMLpackage cn.bob.ad.propertyimport java.io.IOExceptionimport java.io.Input

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>

热点排行