读取jdbc配置信息
sys-config.xm
<?xml version="1.0"encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@172.15.51.254:1521:ORCL</url>
<username>drp</username>
<password>drp</password>
</db-info>
</config>
ConfigReader.java
package com.yang.dom4j;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigReader {
publicstaticvoid main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/sys-config.xml"));
Element driverNameElt = (Element)document.selectObject("/config/db-info/driver-name");
Element urlElt = (Element)document.selectObject("/config/db-info/url");
Element usernameElt = (Element)document.selectObject("/config/db-info/username");
Element passwordElt = (Element)document.selectObject("/config/db-info/password");
System.out.println(driverNameElt.getStringValue());
System.out.println(urlElt.getStringValue());
System.out.println(usernameElt.getStringValue());
System.out.println(passwordElt.getStringValue());
}
}