java读取xml配置文件(sax)
来源:互联网 发布:苏州凌志软件 编辑:程序博客网 时间:2024/06/11 18:31
xml文件:Conn.xml
<?xml version="1.0" encoding="utf-8"?>
<reportenv>
<datasource>
<host>127.0.0.1</host>
<database>test</database>
<username>root</username>
<password>******</password>
</datasource>
</reportenv>
读取xml文件:ReadConfigXml.java
package classes;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.net.URL;
class ConfigParser extends DefaultHandler {
////定义一个Properties 用来存放属性值
private Properties props;
private String currentSet;
private String currentName;
private StringBuffer currentValue = new StringBuffer();
//构建器初始化props
public ConfigParser() {
this.props = new Properties();
}
public Properties getProps() {
return this.props;
}
//定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来.
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
currentValue.delete(0, currentValue.length());
this.currentName =qName;
}
//这里是将<xxx></xxx>之间的值加入到currentValue
public void characters(char[] ch, int start, int length) throws SAXException {
currentValue.append(ch, start, length);
}
//在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
public void endElement(String uri, String localName, String qName) throws SAXException {
props.put(qName.toLowerCase(), currentValue.toString().trim());
}
}
class ParseXML{
//定义一个Properties 用来存放属性值
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;
}
}
}
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 getHost(){
return props.getProperty("host");
}
public String getDataBase(){
return props.getProperty("database");
}
public String getUserName(){
return props.getProperty("username");
}
public String getPassWord(){
return props.getProperty("password");
}
}
连接数据库:Conn.java
//--------------数据库连接---------------------
class Conn{
private Connection conn = null;
private Statement stmt = null;
public static void main(String args[]){
try{ //加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Success loading Mysql Driver!");
}catch(Exception e){
System.out.println("Error loading Mysql Driver!");
}
ReadConfigXml r = new ReadConfigXml("Conn.xml"); //读取xml文件中数据库相关信息
try{ //连接数据库
conn = DriverManager.getConnection("jdbc:mysql://"+r.getHost()+"/"+r.getDataBase()+"?user="+r.getUserName()+"&password="+r.getPassWord()+"&characterEncoding=gb2312");
System.out.println("Success connect Mysql Database!");
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<reportenv>
<datasource>
<host>127.0.0.1</host>
<database>test</database>
<username>root</username>
<password>******</password>
</datasource>
</reportenv>
读取xml文件:ReadConfigXml.java
package classes;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.net.URL;
class ConfigParser extends DefaultHandler {
////定义一个Properties 用来存放属性值
private Properties props;
private String currentSet;
private String currentName;
private StringBuffer currentValue = new StringBuffer();
//构建器初始化props
public ConfigParser() {
this.props = new Properties();
}
public Properties getProps() {
return this.props;
}
//定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来.
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
currentValue.delete(0, currentValue.length());
this.currentName =qName;
}
//这里是将<xxx></xxx>之间的值加入到currentValue
public void characters(char[] ch, int start, int length) throws SAXException {
currentValue.append(ch, start, length);
}
//在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
public void endElement(String uri, String localName, String qName) throws SAXException {
props.put(qName.toLowerCase(), currentValue.toString().trim());
}
}
class ParseXML{
//定义一个Properties 用来存放属性值
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;
}
}
}
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 getHost(){
return props.getProperty("host");
}
public String getDataBase(){
return props.getProperty("database");
}
public String getUserName(){
return props.getProperty("username");
}
public String getPassWord(){
return props.getProperty("password");
}
}
连接数据库:Conn.java
//--------------数据库连接---------------------
class Conn{
private Connection conn = null;
private Statement stmt = null;
public static void main(String args[]){
try{ //加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Success loading Mysql Driver!");
}catch(Exception e){
System.out.println("Error loading Mysql Driver!");
}
ReadConfigXml r = new ReadConfigXml("Conn.xml"); //读取xml文件中数据库相关信息
try{ //连接数据库
conn = DriverManager.getConnection("jdbc:mysql://"+r.getHost()+"/"+r.getDataBase()+"?user="+r.getUserName()+"&password="+r.getPassWord()+"&characterEncoding=gb2312");
System.out.println("Success connect Mysql Database!");
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
- java读取xml配置文件(sax)
- java读取xml配置文件(sax)
- XML配置文件的读取(sax)
- java读取xml配置文件
- java读取xml配置文件
- JAVA读取XML配置文件
- java读取xml配置文件
- java读取XML配置文件
- java读取xml配置文件
- Java SAX解析器解析XML配置文件
- Java读取XML文件之SAX篇
- 在java中sax读取远程xml
- JAVA读取XML之SAX方式
- java读取xml文档之sax方式
- java xml作为配置文件,读取
- java读取xml配置文件(小结)
- java读取xml配置文件(小结)
- java读取xml/.properties配置文件
- 【翻译】如何让flex组件看起来更美观
- 搜索引擎优化排名方法
- 程序员的基本素质
- 实例构造与结构构造(Java/C#)
- 今天去游玩啦..
- java读取xml配置文件(sax)
- 【来自KENSHEN】Flex Dict Online Beta 1.0
- 普通高等学校高职高专教育指导性专业目录专业简介
- 关于提高自己Java水平的十大技术讨论
- 去除Dr.COM 宽带登录客户端路由限制,实现一个帐号多台机器上网
- 一个stmt多个rs进行操作引起的ResultSet错误
- 最近看到一篇有关《Java的未来与发展》A
- 关于页面中获取用户控件中的控件 事件的方法
- java访问数据库