Java本身可以读取properties文件,不需要引入任何jar包。
但是读取XMl文件,最简单并且容易理解的方式可以利用Dom4J方式
首先需要引入:D:\STARK\编程软件JAR文件\dom4j\dom4j-1.6.1.jar,D:\STARK\编程软件JAR文件\dom4j\jaxen-1.1-beta-6.jar
xmL文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@localhost:1521:oracle</url>
<username>drp</username>
<password>drp</password>
</db-info>
</config>
取得文件:
package com.drp.util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 读取XML配置文件sys-config.xml
*
* @author WangYue
*
*/
public class ConfigReader {
private static ConfigReader instance = new ConfigReader();
private Document doc;
private JdbcInfo jdbcInfo;
private ConfigReader() {
try {
doc = new SAXReader().read(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(
"sys-config.xml"));
Element driverNameElement = (Element) doc
.selectObject("/config/db-info/driver-name");
Element urlElement = (Element) doc
.selectObject("/config/db-info/url");
Element usernameElement = (Element) doc
.selectObject("/config/db-info/username");
Element passwordElement = (Element) doc
.selectObject("/config/db-info/password");
jdbcInfo = new JdbcInfo();
jdbcInfo.setDriverName(driverNameElement.getStringValue());
jdbcInfo.setUrl(urlElement.getStringValue());
jdbcInfo.setUsername(usernameElement.getStringValue());
jdbcInfo.setPassword(passwordElement.getStringValue());
} catch (DocumentException e) {
e.printStackTrace();
}
}
public JdbcInfo getJdbcInfo() {
return jdbcInfo;
}
public static ConfigReader getInstance() {
return instance;
}
public static void main(String[] args) {
System.out.print(ConfigReader.getInstance().getJdbcInfo());
}
}
运行结果为:
com.drp.util.JdbcInfo{driverName:oracle.jdbc.driver.OracleDriver,url:jdbc:oracle:thin:@localhost:1521:oracle,username:drp,password:drp}
但是读取XMl文件,最简单并且容易理解的方式可以利用Dom4J方式
首先需要引入:D:\STARK\编程软件JAR文件\dom4j\dom4j-1.6.1.jar,D:\STARK\编程软件JAR文件\dom4j\jaxen-1.1-beta-6.jar
xmL文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@localhost:1521:oracle</url>
<username>drp</username>
<password>drp</password>
</db-info>
</config>
取得文件:
package com.drp.util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 读取XML配置文件sys-config.xml
*
* @author WangYue
*
*/
public class ConfigReader {
private static ConfigReader instance = new ConfigReader();
private Document doc;
private JdbcInfo jdbcInfo;
private ConfigReader() {
try {
doc = new SAXReader().read(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(
"sys-config.xml"));
Element driverNameElement = (Element) doc
.selectObject("/config/db-info/driver-name");
Element urlElement = (Element) doc
.selectObject("/config/db-info/url");
Element usernameElement = (Element) doc
.selectObject("/config/db-info/username");
Element passwordElement = (Element) doc
.selectObject("/config/db-info/password");
jdbcInfo = new JdbcInfo();
jdbcInfo.setDriverName(driverNameElement.getStringValue());
jdbcInfo.setUrl(urlElement.getStringValue());
jdbcInfo.setUsername(usernameElement.getStringValue());
jdbcInfo.setPassword(passwordElement.getStringValue());
} catch (DocumentException e) {
e.printStackTrace();
}
}
public JdbcInfo getJdbcInfo() {
return jdbcInfo;
}
public static ConfigReader getInstance() {
return instance;
}
public static void main(String[] args) {
System.out.print(ConfigReader.getInstance().getJdbcInfo());
}
}
运行结果为:
com.drp.util.JdbcInfo{driverName:oracle.jdbc.driver.OracleDriver,url:jdbc:oracle:thin:@localhost:1521:oracle,username:drp,password:drp}