dom4j解析XML配置文件(例子)

sys-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
   <db-info>
    <driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
    <url>jdbc:oracle:thin:@localhost:1521:HUGUI</url>
    <user-name>drp1</user-name>
    <password>drp1</password>
   </db-info>
   
   <dao-factory>
   <!-- 
    <item-dao-factory>com.hugui.drp.basedata.dao.ItemDaoFactory4Oracle</item-dao-factory>
     -->
    <item-dao-factory>com.hugui.drp.basedata.dao.ItemDaoFactory4MySql</item-dao-factory>
    <user-dao-factory>com.hugui.drp.basedata.dao.ItemDaoFactory4Oracle</user-dao-factory>
   </dao-factory>
</config>




XmlConfigReader.java

package com.hugui.drp.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 用单例模式解析sys-config.xml文件
 * @author Administrator
 *
 */

public class XmlConfigReader {
 
// //饿汉式
// private static XmlConfigReader instance = new XmlConfigReader();
//
// private XmlConfigReader(){
//  
// }
// 
// public static XmlConfigReader getInstance(){
//  return instance;
// }
 
 //满汉式(延迟加载lazy)
 private static XmlConfigReader instance = null;
 
 //保存dao工厂的名称
 //key=名称 value=具体类完整路径
 private Map<String, String> daoFactoryMap = new HashMap<String, String>();
 
 //保存jdbc相关配置信息
 private JdbcConfig jdbcConfig = new JdbcConfig();
 
 private XmlConfigReader(){

  SAXReader reader = new SAXReader();
  InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml");
  try {
   Document doc = reader.read(in);
   Element driverNameElt = (Element)doc.selectObject("/config/db-info/driver-name");
   Element urlElt = (Element)doc.selectObject("/config/db-info/url");
   Element userNameElt = (Element)doc.selectObject("/config/db-info/user-name");
   Element passwordElt = (Element)doc.selectObject("/config/db-info/password");
   //设置jdbc相关的配置
   jdbcConfig.setDirverName(driverNameElt.getStringValue());
   jdbcConfig.setUrl(urlElt.getStringValue());
   jdbcConfig.setUserName(userNameElt.getStringValue());
   jdbcConfig.setPassword(passwordElt.getStringValue());
   
   System.out.println("读取jdbcConfig----->>" + jdbcConfig);
   //读取DaoFactory信息
   List daoFactoryList = doc.selectNodes("/config/dao-factory/*");
   for(int i=0; i<daoFactoryList.size(); i++){
    Element daoFactoryElt = (Element)daoFactoryList.get(i);
    String tagName = daoFactoryElt.getName();
    String tagText = daoFactoryElt.getText();
       System.out.println("读取jdbcConfig----->>" + tagText);
    //放入Map
    daoFactoryMap.put(tagName, tagText);
   }
   
  } catch (DocumentException e) {
   e.printStackTrace();
  }
  
 }
 
 public static synchronized XmlConfigReader getInstance(){
  if(instance == null){
   instance = new XmlConfigReader();
  }
  return instance;
 }
 /**
  * 返回jdbc相关配置
  * @return
  */
 public JdbcConfig getJdbcConfig(){
  return jdbcConfig;
 }
 /**
  * 根据标签名称取得DaoFactory的名字
  * 
  * @param daoFactoryName
  * @return daoFactory的完整类路径 
  */
 public String getDaoFactory(String daoFactoryName){
  return daoFactoryMap.get(daoFactoryName);
 }
 
 public static void main(String [] args){
  
  String itemDaoFactory = XmlConfigReader.getInstance().getDaoFactory("item-dao-factory");
  System.out.println(itemDaoFactory);
//  JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig();
//  System.out.println(jdbcConfig.toString());
 }

}


 


你可能感兴趣的:(java,DAO,oracle,xml,jdbc)