java解析xml

题计:这里给出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("测试成功!");
}

你可能感兴趣的:(java,spring,sql,xml,mysql)