采用apache的commons.digester包对XML文件进行简单映射解析!

用Xstream也可以实现类似的功能,但XStream对标签属性的解析比较麻烦,用apache的commons.digester就方便多了,举例如下:

用之前要先包含如下几个包:

                commons-digester.jar

     要解析的xml文件如下(server.xml):

<?xml version="1.0" encoding="gb2312"?>
<config>
       
        <database-server supply="MYSQL" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/aws?characterEncoding=gb2312" username="root" password="password" debug="on" />
        
        <diy diy.parameter1="" diy.parameter2="22" diy.parameter3="" diy.parameter4="" diy.parameter5="" diy.parameter6="" diy.parameter7="" diy.parameter8="" />
        
        <optimize max.client="500" thread.overtime="1200000" database.max.session="200000" database.pool.init="5" database.pool.max.workload="30000" />
        
        <log runtime.server="on" runtime.filename="../logs/access.log" error.filename="../logs/error.log" />
        
        <plus-httpd supply="jboss" server.root="../webserver/webapps/portal/" security.code="AH{%C1%F5%BD%F0%D6%F9}" />
</config>

解析代码:

        File localFile = new File("system.xml");
        Digester localDigester;
        (localDigester = new Digester()).setValidating(false);

        localDigester.addObjectCreate("config", AWFConfig.class);

        localDigester.addObjectCreate("config/database-server", DataBaseServerConf.class);
        localDigester.addSetProperties("config/database-server", "supply", "supply");
        localDigester.addSetProperties("config/database-server", "driver", "driver");
        localDigester.addSetProperties("config/database-server", "url", "url");
        localDigester.addSetProperties("config/database-server", "username", "userName");
        localDigester.addSetProperties("config/database-server", "password", "password");
        localDigester.addSetProperties("config/database-server", "debug", "debug");
        localDigester.addSetNext("config/database-server", "setDataBaseServerConf");

        localDigester.addObjectCreate("config/schedule", ScheduleConf.class);
        localDigester.addSetProperties("config/schedule", "task.cost", "taskCost");
        localDigester.addSetProperties("config/schedule", "plan.service", "planService");
        localDigester.addSetNext("config/schedule", "setScheduleConf");

        localDigester.addObjectCreate("config/diy", DIYConf.class);
        localDigester.addSetProperties("config/diy", "diy.parameter1", "diyParameter1");
        localDigester.addSetProperties("config/diy", "diy.parameter2", "diyParameter2");
        localDigester.addSetProperties("config/diy", "diy.parameter3", "diyParameter3");
        localDigester.addSetProperties("config/diy", "diy.parameter4", "diyParameter4");
        localDigester.addSetProperties("config/diy", "diy.parameter5", "diyParameter5");
        localDigester.addSetProperties("config/diy", "diy.parameter6", "diyParameter6");
        localDigester.addSetProperties("config/diy", "diy.parameter7", "diyParameter7");
        localDigester.addSetProperties("config/diy", "diy.parameter8", "diyParameter8");
        localDigester.addSetNext("config/diy", "setDIYConf");

        localDigester.addObjectCreate("config/optimize", OptimizeConf.class);
        localDigester.addSetProperties("config/optimize", "max.client", "maxClient");
        localDigester.addSetProperties("config/optimize", "thread.overtime", "threadOvertime");
        localDigester.addSetProperties("config/optimize", "database.max.session", "maxSession");
        localDigester.addSetProperties("config/optimize", "database.pool.init", "databasePoolInit");
        localDigester.addSetProperties("config/optimize", "database.pool.max.workload", "databasePoolMaxWorkload");
        localDigester.addSetNext("config/optimize", "setOptimizeConf");

        localDigester.addObjectCreate("config/log", LogConf.class);
        localDigester.addSetProperties("config/log", "runtime.server", "runtimeServer");

        localDigester.addSetNext("config/log", "setLogConf");

        localDigester.addObjectCreate("config/plus-httpd", HttpDConf.class);
        localDigester.addSetProperties("config/plus-httpd", "supply", "supply");
        localDigester.addSetProperties("config/plus-httpd", "server.root", "serverRoot");
        localDigester.addSetProperties("config/plus-httpd", "security.code", "securityCode");
        localDigester.addSetNext("config/plus-httpd", "setHttpDConf");
        try {
            return (AWFConfig) localDigester.parse(new FileInputStream(localFile));
        } catch (SAXException localSAXException) {
            throw new ConfigParserException(localSAXException.toString());
        } catch (IOException localIOException) {
            throw new ConfigParserException(localIOException.toString());
        }


映射类AWFConfig:


public class AWFConfig extends Config {

    public static AWFServerConf _awfServerConf;
    public static DataBaseServerConf _databaseServerConf;
    public static OptimizeConf _optimizeConf;
    public static LogConf _logConf;
    public static ScheduleConf _scheduleConf;
    public static HttpDConf _httpdConf;
    public static DIYConf _diyConf;
    private AWFServerConf awfServerConf;
    private DataBaseServerConf dataBaseServerConf;
    private OptimizeConf optimizeConf;
    private LogConf logConf;
    private ScheduleConf scheduleConf;
    private DIYConf diyConf;
    private HttpDConf httpDConf;

    public AWFServerConf getAWFServerConf() {
        return this.awfServerConf;
    }

    public DIYConf getDIYConf() {
        return this.diyConf;
    }

    public DataBaseServerConf getDataBaseServerConf() {
        return this.dataBaseServerConf;
    }

    public HttpDConf getHttpDConf() {
        return this.httpDConf;
    }

    public LogConf getLogConf() {
        return this.logConf;
    }

    public OptimizeConf getOptimizeConf() {
        return this.optimizeConf;
    }

    public ScheduleConf getScheduleConf() {
        return this.scheduleConf;
    }

    public void setAWFServerConf(AWFServerConf paramAWFServerConf) {
        this.awfServerConf = paramAWFServerConf;
    }

    public void setDIYConf(DIYConf paramDIYConf) {
        this.diyConf = paramDIYConf;
    }

    public void setDataBaseServerConf(DataBaseServerConf paramDataBaseServerConf) {
        this.dataBaseServerConf = paramDataBaseServerConf;
    }

    public void setHttpDConf(HttpDConf paramHttpDConf) {
        this.httpDConf = paramHttpDConf;
    }

    public void setLogConf(LogConf paramLogConf) {
        this.logConf = paramLogConf;
    }

    public void setOptimizeConf(OptimizeConf paramOptimizeConf) {
        this.optimizeConf = paramOptimizeConf;
    }

    public void setScheduleConf(ScheduleConf paramScheduleConf) {
        this.scheduleConf = paramScheduleConf;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值