題計:這里給出java解析xml,以幫助人們理解許多容器是怎么做的。。像spring,struts等.
1.mysql.xml代碼:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<datasource>
<servername>localhost</servername>
<serverport>3306</serverport>
<databasename>juddi</databasename>
<username>root</username>
<password>123456</password>
</datasource>
</data>
2.XML配置文件解析器,主要目的,是為做前期工作
package com;
/*
* XML配置文件解析器,主要目的,是為做前期工作
*/
import org.xml.sax.helpers.DefaultHandler;
import java.util.Properties;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public class ConfigParser extends DefaultHandler {
//定義一個properties用來存放屬性
private Properties props;
private String currentName;
private StringBuffer currentValue=new StringBuffer();
public ConfigParser(){
this.props=new Properties();
}
public Properties getProps(){
return this.props;
}
//這里是將xml中元素值加入currentValue
public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue.append(ch, start, length);
}
//在遇到</xx>時,將之間的字符存放在props中間
public void endElement(String uri, String localName, String name)
throws SAXException {
props.put(currentName.toLowerCase(), currentValue.toString().trim());
}
//定義開始解析元素的方法,這里將<xx>中的名稱xx提出來,
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentValue.delete(0, currentValue.length());
currentName=qName;
}
//
}
3.XML配置文件計取處理
package com;
/*
* XML配置文件計取處理
*/
import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class ParseXML {
//定義一個Proerties用來存放屬性值
private Properties props;
public Properties getProps(){
return this.props;
}
public void parse(String filename)throws Exception{
//將我們的解析器對象化
ConfigParser handler=new ConfigParser();
//獲取SAX工廠對象
SAXParserFactory factory=SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
//獲取SAX解析
SAXParser parser=factory.newSAXParser();
try{
//將解析器和解析對象xml聯系起來,開始解析
parser.parse(filename, handler);
//獲取解析成功后的屬性
props=handler.getProps();
}finally{
factory=null;
parser=null;
handler=null;
}
}
}
4.讀取XML配置文件
package com;
/*
* 讀取XML配置文件
*/
import java.util.Properties;
public class ReadConfigXml {
private Properties props;
public ReadConfigXml(String url){
ParseXML myRead=new ParseXML();
try{
myRead.parse(url);
props=new Properties();
props=myRead.getProps();
}catch(Exception e){
e.printStackTrace();
}
}
public String getServerName(){
return props.getProperty("servername");
}
public String getServerPort(){
return props.getProperty("serverport");
}
public String getDatabaseName(){
return props.getProperty("databasename");
}
public String getUserName(){
return props.getProperty("username");
}
public String getPassword(){
return props.getProperty("password");
}
}
5.數據庫連接加測試數據庫連接加測試數據庫連接加測試
package com;
/*
*
* 數據庫連接加測試數據庫連接加測試數據庫連接加測試
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private Connection con;
private DBConnection(){
}
public static DBConnection newInstance(){
return new DBConnection();
}
public Connection getConnection(){
ReadConfigXml r=new ReadConfigXml("src/mysql.xml");
String url="jdbc:mysql://"+r.getServerName()+":"+r.getServerPort()+"/"+r.getDatabaseName();
String username=r.getUserName();
String password=r.getPassword();
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return con;
}
//測試連接
public static void main(String args[]){
Connection con=DBConnection.newInstance().getConnection();
System.out.println("測試成功!");
}
}
posted on 2009-09-08 12:51
Werther 閱讀(340)
評論(0) 編輯 收藏