Digester解析xml到bean

参考网站:http://www.blogjava.net/alex/archive/2006/09/06/68148.html
模拟tomcat中XML文件的解析:
文件夹名为:lis
package com.yidu.lis.bean;

import java.io.File;

import org.apache.commons.digester.Digester;

import com.yidu.lis.vo.DBServerVo;

/**
* CopyRight(C),2011-2012,一度教育
* FileName:com.yidu.lis.bean.WebAppConfigManager.java
* <p>
* <B>贷款计息查询服务器系统</B>
* </p>
*
* @author student
* @Date Apr 26, 20112011-4-19 上午12:23:57 PM
* @Description 属性文件的配置管理
* @version 1.00 类创建者:肖祺
* @see 参考类
*/
public class WebAppConfigManager {
private final static String cfgFile = System.getProperty("user.dir")
+ java.io.File.separator + "xml\\bank.xml";
private WebApp webApp = null;
private static WebAppConfigManager wcm = null;

private WebAppConfigManager() throws Exception {
parseWebXMLUseDigester();
}

public static WebAppConfigManager getWcm() throws Exception {
if (wcm==null) {
wcm = new WebAppConfigManager();
}
return wcm;
}

public WebApp getWebApp() {
return webApp;
}

void parseWebXMLUseJDOM() throws Exception {

}

/**
* 用Digester进行解析
*
* @throws Exception
*/
void parseWebXMLUseDigester() throws Exception {
//创建解析文件对象
Digester digester = new Digester();
digester.setValidating(false);
//创建根节点
digester.addObjectCreate("web-app", WebApp.class);
digester.addBeanPropertySetter("web-app/description", "description");
//创建Servlet节点
digester.addObjectCreate("web-app/servlet", BusinessServlet.class);
digester.addBeanPropertySetter("web-app/servlet/servlet-name","servletName");
digester.addBeanPropertySetter("web-app/servlet/servlet-class","servletClass");
digester.addSetNext("web-app/servlet", "addBusinessServlet");
//创建InitParam节点
digester.addObjectCreate("web-app/servlet/init-param",InitParam.class);
   digester.addBeanPropertySetter("web-app/servlet/init-param/param-name","paramName");
   digester.addBeanPropertySetter("web-app/servlet/init-param/param-value","paramValue");
        digester.addSetNext("web-app/servlet/init-param","addInitParam");
   //创建servlet-mapping节点
digester.addObjectCreate("web-app/servlet-mapping",ServletMapping.class);
digester.addBeanPropertySetter("web-app/servlet-mapping/servlet-name","servletName");
digester.addBeanPropertySetter("web-app/servlet-mapping/url-pattern","urlPattern");
digester.addSetNext("web-app/servlet-mapping", "addServletMapping");
File file = new File(cfgFile);
//创建数据库节点
digester.addObjectCreate("web-app/resource",DBServerVo.class);
digester.addSetProperties("web-app/resource", "name","name");
                digester.addSetProperties("web-app/resource", "maxActive","maxActive");
                digester.addSetProperties("web-app/resource", "maxIdle","maxIdle");
                digester.addSetProperties("web-app/resource", "maxWait","maxWait");
                digester.addSetProperties("web-app/resource", "userName","userName");
                digester.addSetProperties("web-app/resource", "passWord","passWord");
                digester.addSetProperties("web-app/resource", "driverClassName","driverClassName");
                digester.addSetProperties("web-app/resource", "url","url");
digester.addSetNext("web-app/resource", "addConfigDataSource");
webApp = (WebApp) digester.parse(file);
}

}

你可能感兴趣的:(tomcat,bean,xml,Web,servlet)