用Digester解析xml

假如有如下XML文件:

<!---->

 <code>

<pageList>
 <bizBase name="业务申报-基本信息">
  <item bizNature="1" bizName="一般业务"     bizURL="maintainbizcommonbiz.jsf"/>
  <item bizNature="2" bizName="项目类组合贷款业务"   bizURL="maintainbizcombinationloanbiz.jsf"/>
  <item bizNature="3" bizName="贸易融资额度合同项下业务" bizURL="maintainbizcombinationloanbiz2.jsf"/>
  </bizBase>

 <bizDetail1 name="业务申报-业务明细-界面1">
  <items bizName="一般业务">                                                                                      
   <item bizNature="1"  productCode="01001001" productName="土地储备贷款" bizURL="groundhiveloan1.jsf"/>     
   <item bizNature="1"  productCode="01001002" productName="房地产开发"   bizURL="groundhiveloan1.jsf"/>    
   <item bizNature="1"  productCode="01002001" productName="基本建设贷款" bizURL="groundhiveloan1.jsf"/>     
    
  </items>
 </bizDetail1>
<bizDetail2 name="授信品种-业务明细-界面2">
  <items productName="土地储备贷款">
   <item productCode="01001001" bizNature="1" bizName="一般业务"           bizURL="genbiz.jsf"/>
   <item productCode="01001001" bizNature="2" bizName="项目类组合贷款业务"     bizURL="genbiz.jsf"/>
   <item productCode="01001001" bizNature="3" bizName="贸易融资额度合同项下业务"      bizURL="genbiz.jsf"/>
   </items>                                                                                                          
  </bizDetail2>  
<bizDetail3 name="授信品种-业务明细表">
  <items bizTypeName="业务基本信息">
   <item bizDetailType="1" productCode="01001001" productName="土地储备贷款"      excelTemplateCode="YWSB_1" />
   <item bizDetailType="1" productCode="01001002" productName="房地产开发"       excelTemplateCode="YWSB_3" />
   <item bizDetailType="1" productCode="01002001" productName="基本建设贷款"      excelTemplateCode="YWSB_2" />
   </items>
  
 </bizDetail3>

</pageList>
        
    </code> 

解析代码:

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

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import cn.ccb.clpm.bizprocess.myjob.config.BusinessType;
import cn.ccb.clpm.bizprocess.myjob.config.BusinessTypeConfig;
import cn.ccb.clpm.common.model.vo.bizapplication.BizElementVO;


/**
 * 此类用来从配置文件读取业务类型定义及业务类型与工作列表类型对应关系
 * @version 1.0
 *
 */
public class BizApplicationPageConfig {
 
 private static final  String _BizApplicationPageConfig="/cn/ccb/clpm/common/bizprocess/loanorigination/bizapplication/BizApplicationConfig.xml";
 private static Digester digester;
 private static BizApplicationPageConfig instance=null;
 private List bizBaseElementList = new ArrayList();
 private List bizDetailElementList_1 = new ArrayList();
 private List bizDetailElementList_2 = new ArrayList();
 //private static final List bizNatureProductRelation = new ArrayList();
 private List bizDetailTypeProductRelation = new ArrayList();

    public static BizApplicationPageConfig getInstance()throws Exception{
     if (instance == null) {
      instance = new BizApplicationPageConfig();
      init();
        }
     return instance;
    }
   
 private static void  init() throws IOException,SAXException,Exception{
  InputStream is = BizApplicationPageConfig.class.getResourceAsStream(_BizApplicationPageConfig);
  digester = new Digester();
  digester.push(BizApplicationPageConfig.getInstance());
  digester.setValidating(false);
  digester.setNamespaceAware(false);
        addBizElementRules(digester,"pageList/bizBase/item","addBizBaseElementList");
        addBizElementRules(digester,"pageList/bizDetail1/items/item","addBizDetailElementList1");
        addBizElementRules(digester,"pageList/bizDetail2/items/item","addBizDetailElementList2");
        addBizElementRules(digester,"pageList/bizDetail3/items/item","addBizDetailTypeProductRelation");
        digester.parse(is);      
 }

    private static void addBizElementRules(Digester digester,String model,String method) {
     digester.addObjectCreate(model,BizElementVO.class);
     digester.addSetProperties(model);
     digester.addSetNext(model, method); 
       
    }
   
 public void addBizBaseElementList(BizElementVO bizElement){
  bizBaseElementList.add(bizElement);
 }
 
    public List getBizBaseElementList(){
     return  bizBaseElementList;
    }
   
    public void addBizDetailElementList1(BizElementVO bizElement){
     bizDetailElementList_1.add(bizElement);
 }
 
    public List getBizDetailElementList1(){
     return  bizDetailElementList_1;
    }
   
    public void addBizDetailElementList2(BizElementVO bizElement){
     bizDetailElementList_2.add(bizElement);
 }
 
    public List getBizDetailElementList2(){
     return  bizDetailElementList_2;
    }
   
    public void addBizDetailTypeProductRelation(BizElementVO bizElement){
     bizDetailTypeProductRelation.add(bizElement);
 }
 
    public List getBizDetailTypeProductRelation(){
     return  bizDetailTypeProductRelation;
    }

// public static void main(String[] args)throws Exception {
//  List l = BizApplicationPageConfig.getInstance().getBizBaseElementList();
//  for(int i=0;i //   //System.out.println(((BizElementVO)l.get(i)).getBizURL());
//  }
//  l = BizApplicationPageConfig.getInstance().getBizDetailElementList1();
//  //System.out.println("l.size()="+l.size());
//  for(int i=0;i //   //System.out.println(((BizElementVO)l.get(i)).getBizURL());
//  }
//  
//  l = BizApplicationPageConfig.getInstance().getBizDetailElementList2();
//  //System.out.println("l.size()="+l.size());
//  for(int i=0;i //   //System.out.println(((BizElementVO)l.get(i)).getBizURL());
//  }
//  
//  l = BizApplicationPageConfig.getInstance().getBizNatureProductRelation();
//  //System.out.println("l.size()="+l.size());
//  for(int i=0;i //   //System.out.println(((BizElementVO)l.get(i)).getBizURL());
//  }
// } 
  

}

 

你可能感兴趣的:(apache,xml,工作,JSF)