论坛首页 Java企业应用论坛

java中org.apache.commons.digester类用法

浏览 2462 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-28  

xml文件内容:

 

<?xml version="1.0" encoding="UTF-8"?>  
  
<channels>  
  <channel switchTime="20080101000101" k="1.07" n="6" s="55" channelId="0404" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0405" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0406" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0407" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0408" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0409" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0433" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0434" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0437" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0435" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0438" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0439" channelName="xxxx.xxxx.xxx.cn"/>  
  <channel switchTime="20080301010101" k="1.07" n="6" s="55" channelId="0440" channelName="xxxx.xxxx.xxx.cn"/>  
</channels>  

 

import org.apache.commons.digester.Digester;           
Digester digester = new Digester();   
//此方法作用是是否对XML文档进行DTD验证   
digester.setValidating(false);   
//addObjectCreate的作用是:当遇见channels元素的时候,产生一个java.util.ArrayList对象   
digester.addObjectCreate("channels", "java.util.ArrayList");   
//addSetProperties作用是:对channels元素所含的属性赋值(即下面channels/channel中的对象值)   
digester.addSetProperties("channels");   
//当遇见channels/channel元素的时候,产生一个com.chinacache.config.model.ChannelSwitch对象   
digester.addObjectCreate("channels/channel","com.chinacache.config.model.ChannelSwitch");   
//对channels/channel元素所含的属性赋值给ChannelSwitch对象       
digester.addSetProperties("channels/channel");   
//当再一次遇见 channels下的channel元素的时候,调用ArrayList类的add()方法,add:com.chinacache.config.model.ChannelSwitch对象           
digester.addSetNext("channels/channel", "add","com.chinacache.config.model.ChannelSwitch");  


 

 

 

 

调用:

 

 

try {   
  
            List channelList = (List) digester   
                    .parse(ChannelSwitchInfoImpl.class  
                            .getResourceAsStream(Constants.PATH_CHANNEL_SWITCH));   
            for (int i = 0; i < channelList.size(); i++) {   
                ChannelSwitch channelSwitch = (ChannelSwitch) channelList   
                        .get(i);   
                this.channelSwitchs.put(channelSwitch.getChannelName(),   
                        channelSwitch);   
                this.channelSwitchsSavedById.put(channelSwitch.getChannelId(),   
                        channelSwitch);   
            }   
  
               
  
        } catch (IOException e) {   
            logger.error("load channelswitch.xml error.", e);   
        } catch (SAXException e) {   
            logger.error("load channelswitch.xml error.", e);   
        }  


 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics