读取xml 文档

例子:

  

//先new xml  读取
private static final String CONFIG_PATH="configuration.xml";
private static final XmlReader xml=new XmlReader(CONFIG_PATH);

public List<CheckBean> jobsel=Constant.rzPerSelJob;
public static final List<CheckBean> rzPerSelJob=new ArrayList<CheckBean>();
 //静态赋值
static{
      for(String s:xml.getStringArray("rzPer.select.job")){
          String[] t=s.split(":");  
          rzPerSelDegree.add(new CheckBean(1, t[0],t[1], "degree-sel"));
    }
       
}    ///


    xml 文档写法


<?xml version="1.0" encoding="GB2312"?>
<configuration>

                 <rzPer>
                        <select>
                             <job>01:学习</job>
                             <job>02:企事业单位职工</job>
                <job>03:农民</job>
                <job>04:个体工商户</job>
                <job>05:学生</job>
                  <job>06:无业</job>
                <job>07:其他</job>
                             </select>
                  </rzPer>
         
</configuration>        



//xml  bean 

package com.yuhong.see.util;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class XmlReader {
    private String  CONFIG_PATH;
    
    private static Configuration config; 
    
    public XmlReader(String ConfigPath){
        this.CONFIG_PATH=ConfigPath;
        this.Configure();
    }
    
    public void Configure(){
        try {
            config = new XMLConfiguration(CONFIG_PATH);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    public String getString(String infoLabel) {
        return config.getString(infoLabel);
    }
    
    public boolean getBoolean(String infoLabel) {
        return config.getBoolean(infoLabel);
    }
    
    public int getInt(String infoLabel) {
        return config.getInt(infoLabel);
    }
    
    public double getDouble(String infoLabel) {
        return config.getDouble(infoLabel);
    }
    
    public String[] getStringArray(String infoLabel) {
        return config.getStringArray(infoLabel);
    }
}                                                                                                                                                                            

 

posted on 2013-04-18 15:21  远方的人  阅读(224)  评论(0编辑  收藏  举报

导航