使用Digester解析XML文件和JAVA对象的映射

本文介绍了一个Java应用程序如何通过使用Digester进行XML配置文件解析,并创建和管理DAO对象的过程。通过具体的代码实现,展示了如何初始化配置对象,加载DAO配置,以及异常处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
package cn.hxex.exam.config;

import java.util.Hashtable;

import cn.hxex.exam.exception.ExamException;

public class ExamConfig {
   
private Hashtable daos;
   
public ExamConfig(){
       daos
=new Hashtable();
   }

   
public void addDao(DAOConfig bean){
       daos.put(bean.getId(), bean);
   }

   
   
public DAOConfig getDAOConfig(String name){
       DAOConfig config
=(DAOConfig)daos.get(name);
       
if(config==null){
           
throw new ExamException("couldn't find the bean:"+name);
       }

       
return config;
   }

}


package cn.hxex.exam.config;

import java.io.InputStream;

import org.apache.commons.digester.Digester;

public class ExamConfigUtil {
   
public static final String DEFAULT_CONFIG_FILE="ExamConfig.xml";
   
public static final ExamConfig MESSAGE_CONFIG;
   
   
static{
       Digester digester
=new Digester();
       digester.setValidating(
false);
       
       
//初始化根配置对象
       digester.addObjectCreate("config""cn.hxex.exam.config.Examconfig");
       digester.addSetProperties(
"config");
       
       
//初始化DAOConfig对象
       digester.addObjectCreate("config/daos/dao""cn.hxex.exam.config.DAOConfig");
       digester.addSetProperties(
"config/daos/dao");
       digester.addSetNext(
"config/dao/dao""addDao","cn.hxex.exam.config.DAOConfig");
       
       
//读取配置文件
       ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
       InputStream in
=classLoader.getResourceAsStream(DEFAULT_CONFIG_FILE);
       ExamConfig config
=null;
       
try {
          
if(in!=null){
              config
=(ExamConfig)digester.parse(in);
          }

        }
 catch (Exception e) {
          e.printStackTrace();
       }

        MESSAGE_CONFIG
=config;    
   }

   
   
public static ExamConfig getConfig(){
       
return ExamConfigUtil.MESSAGE_CONFIG;
   }

   
   
public static DAOConfig getDAOConfig(String name){
       
return MESSAGE_CONFIG.getDAOConfig(name);
   }

   
}


<config>
    
<daos>
        
<dao id="UserDAO" type="cn.hxex.exam.dao.hibernate.UserDAOHibernate" />
        
<dao id="TeacherDAO" type="cn.hxex.exam.dao.hibernate.TeacherDAOHibernate" />
        
<dao id="TestPaperDAO" type="cn.hxex.exam.dao.hibernate.TestPaperDAOHibernate" />
        
<dao id="RoleDAO" type="cn.hxex.exam.dao.hibernate.RoleDAOHibernate" />
        
<dao id="ClassesDAO" type="cn.hxex.exam.dao.hibernate.ClassesDAOHibernate" />
        
<dao id="StudentDAO" type="cn.hxex.exam.dao.hibernate.StudentDAOHibernate" />
        
<dao id="SelectQuestionDAO" type="cn.hxex.exam.dao.hibernate.SelectQuestionDAOHibernate" />
        
<dao id="YesNoQuestionDAO" type="cn.hxex.exam.dao.hibernate.YesNoQuestionDAOHibernate" />
    
</daos>
    
<cache type="cn.hxex.exam.cache.ehcache.EHCache">
        
<property name="cachename" value="cache" />
    
</cache>
    
<sysconfig>
        
<property name="superuser" value="super,test" />
    
</sysconfig>
</config>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值