Today, with the help of the instance for configuring Oracle database connection, we will learn one way to read xml files (other reading methods will be introduced later): mdash; dom4j
Today, with the help of the instance for configuring Oracle database connection, we will learn one way to read xml files (other reading methods will be introduced later): mdash; dom4j
Today, with the help of the instance for configuring Oracle database connection, we will learn one way to read xml files (other reading methods will be introduced later)-using dom4j + xpath.
Dom4j is an open-source XML parsing package produced by dom4j.org. Therefore, the package "dom4j-1.6.1.jar" must be copied to the web-inf/lib.
We take the xpath method to look up, so we must copy the package "jaxen-1.1-beta-5.jar" to web-inf/lib as well. Otherwise, an exception occurs in java. lang. NoClassDefFoundError: org/jaxen/JaxenException.
Copy the preceding two packages to encode them:
1. The xml configuration file is as follows:
Oracle. jdbc. driver. OracleDriver
Jdbc: oracle: thin: @ localhost: 1521: bjpowernode
Lzq
Lzq
2. Because the configuration file contains drivers, URLs, user names, and passwords, multiple values must be returned after resolution. Therefore, we create a JdbcConfig object class and set the attributes as the above fields to store the information after parsing the xml file.
Package com. bjpowernode. drp. util;
/**
* Jbdc configuration information
* @ Author liuzhengquan
*/
Public class JdbcConfig {
@ Override
// Rewrite the toString Method
Public String toString (){
// The content of the method body is the same as that of the parent class.
// Return super. toString ();
Return this. getClass (). getName () + "{driverName:" + driverName +
", Url:" + url +
", Username:" + username +
", Password:" + password + "}";
}
Private String driverName;
Private String url;
Private String username;
Private String password;
// The following get and set methods can be automatically generated in myEclipse.
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;
}
}