在以前自己使用的xml较少,只是了解其很强大,现在可算是在DRP中,真正的开始使用它了,以前只是简单的理解xml,xml即可扩展标记语言,简单的使用,具体是什么?怎么用?还是一直让自己期待的.
一、兼容现有协议
二、统一的管理数据存取格式
三、不同应用系统间数据的共享和交互
四、底层传输的数据更具可读性
五、它和json都是一种数据交换格式
Dom4j是一个易于使用的,开源的库,在Java平台上与XML,XPath,XSLT协同工作。使用Java集合框架,全面支持DOM,SAX,JAXP。
dom4j是一个Java的XMLAPI,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的JavaXMLAPI,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@127.0.0.1:1521:ORCL</url>
<user-name>sys</user-name>
<password>sys</password>
</db-info>
</config>
import java.io.InputStream;
//引用相应的包
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
package conn.lishehe.drp.util;
import java.io.InputStream;
//引用相应的包
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlConfigReader {
//懒汉式
private static XmlConfigReader instance = null;
//保存jdbc相关配置信息的
private JdbcConfig jdbcConfig = new JdbcConfig();
private XmlConfigReader(){
//建立一个SAXReader对象
SAXReader reader = new SAXReader();
//通过当前线程的类加载器,获得文件的相对路径,读入缓冲输入流
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml");
try {
//取得jdbc相关配置信息
//通过流来读取xml文件
Document doc = reader.read(in);
//读取jdbc相关信息
//Element:用于描述XML文档中的元素。
Element driverNameElt = (Element)doc.selectObject("/config/db-info/driver-name");
Element urlElt = (Element)doc.selectObject("/config/db-info/url");
Element userNameElt = (Element)doc.selectObject("/config/db-info/user-name");
Element passwordElt = (Element)doc.selectObject("/config/db-info/password");
/*String driverName = driverNameElt.getStringValue();
String url = urlElt.getStringValue();
String userName = userNameElt.getStringValue();
@SuppressWarnings("unused")
String password = passwordElt.getStringValue();
System.out.println(driverName);*/
//取得jdbc相关的配置
jdbcConfig.setDriverName(driverNameElt.getStringValue());
jdbcConfig.setUrl(urlElt.getStringValue());
jdbcConfig.setUserName(userNameElt.getStringValue());
jdbcConfig.setPassword(passwordElt.getStringValue());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static synchronized XmlConfigReader getInstance(){
if(instance == null){
instance = new XmlConfigReader();
}
return instance;
}
/**
* 返回jdbc相关配置
* @return
*/
public JdbcConfig getJdbcConfig(){
return jdbcConfig;
}
public static void main(String[] args){
JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig();
/*System.out.println(jdbcConfig.getDriverName());
System.out.println(jdbcConfig.getUrl());
System.out.println(jdbcConfig.getUserName());*/
System.out.println(jdbcConfig);
}
}
package conn.lishehe.drp.util;
/**
* Jdbc
* @author lishe
*
*/
public class JdbcConfig {
private String driverName;
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String url;
private String userName;
private String password;
@Override
public String toString() {
return this.getClass().getCanonicalName() + "{driverName:" + driverName + ", url:" + url + ", userName:" + userName + "}";
}
}
package conn.lishehe.drp.util;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 封装数据库常用操作
* @author 李社河
*/
public class DbUtil {
/**
* 取得Connection
* @return
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws ClassNotFoundException{
/* Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String username = "";
String password = "";
conn = DriverManager.getConnection(url, username,password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;*/
Connection conn = null;
try {
JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig();
Class.forName(jdbcConfig.getDriverName());
conn = DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUserName(),jdbcConfig.getPassword());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void main(String[] args){
try {
//测试获取到相应的配置文件的信息,打出来
System.out.println(DbUtil.getConnection());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//Element:用于描述XML文档中的元素。
Element driverNameElt = (Element)doc.selectObject("/config/db-info/driver-name");
Element urlElt = (Element)doc.selectObject("/config/db-info/url");