java 读取xml
1.DB.java
package com.bn.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DB {
private static String dbServer;
private static String dbName;
private static String dbUser;
private static String dbPwd;
//java 项目 www.fhadmin.org
public void readXML(){
SAXReader sr = new SAXReader();//获取读取xml的对象。
Document doc = null;
String path1 = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));
//System.out.println(path1);
path1 = path1 + "../DB.xml";
//System.out.println(path1);
try {
doc = sr.read(path1);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//得到xml所在位置。然后开始读取。并将数据放入doc中
Element el_root = doc.getRootElement();//向外取数据,获取xml的根节点。
Iterator it = el_root.elementIterator();//从根节点下依次遍历,获取根节点下所有子节点
while(it.hasNext()){//遍历该子节点
Object o = it.next();//再获取该子节点下的子节点
Element el_row = (Element)o;
String s = el_row.getText();
Iterator it_row = el_row.elementIterator();
int i =1;
while(it_row.hasNext()){//遍历节点
Element el_ename = (Element)it_row.next();//获取该节点下的所有数据。
if(i == 1){
this.setDbServer(el_ename.getText());
//System.out.println(this.getDbServer());
//dbServer = el_ename.getText();
}
if(i == 2){
this.setDbName(el_ename.getText());
// System.out.println(this.getDbName());
//dbName = el_ename.getText();
}
if(i == 3){
this.setDbUser(el_ename.getText());
// System.out.println(this.getDbUser());
//dbUser = el_ename.getText();
}
if(i == 4){
this.setDbPwd(el_ename.getText());
// System.out.println(this.getDbPwd());
//dbPwd = el_ename.getText();
}
// System.out.println(i);
i++;
//System.out.println(el_ename.getText());
}
//System.out.println(o);
}
}
static {
DB dbxml = new DB();
dbxml.readXML();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection createConn(){
DB dbxml = new DB();
//String name = dbxml.dbName;
//System.out.println(name);
//System.out.println(dbxml.getDbServer() + dbxml.getDbName() + dbxml.getDbUser() + dbxml.getDbPwd());
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://"+ dbxml.getDbServer() +":3306/"+ dbxml.getDbName() +"?user="+ dbxml.getDbUser() +"&password="+ dbxml.getDbPwd());
//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bnss?user=root&password=root");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement createPstmt(Connection conn, String sql){
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn){
if(conn == null)return;
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt){
try {
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs){
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public String getDbServer() {
return dbServer;
}
public String getDbUser() {
return dbUser;
}
public String getDbPwd() {
return dbPwd;
}
public void setDbServer(String dbServer) {
this.dbServer = dbServer;
}
public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}
public void setDbPwd(String dbPwd) {
this.dbPwd = dbPwd;
}
}