- package com.zhengxin.eoms.check.common.util;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
- import com.zhengxin.eoms.check.service.exception.KhException;
- /**
- * xml配置文件解析类
- * @author Administrator
- *
- */
- public class XmlUtil {
- private InputStream is;
- private Map datas=new HashMap();
- public XmlUtil(String configFilePath){
- if(configFilePath==null){
- throw new KhException("没有初始化配置文件");
- }
- try{
- is=this.getClass().getResourceAsStream(configFilePath);
- }catch(Exception e){
- throw new KhException("找不到配置文件");
- }
- init();
- }
- private void init(){
- SAXReader sax=new SAXReader();
- try{
- Document doc=sax.read(is);
- List dicList=doc.selectNodes("/dictionary/dic");
- Iterator it1=dicList.iterator();
- while(it1.hasNext()){
- Element dic=(Element) it1.next();
- String type=dic.attributeValue("type");
- // System.out.println("type="+type);
- List data=new ArrayList();
- List wordList=dic.elements();
- Iterator it2=wordList.iterator();
- while(it2.hasNext()){
- Element word=(Element) it2.next();
- String code=word.attributeValue("code");
- String name=word.getTextTrim();
- // System.out.println("code="+code+" name="+name);
- ConfigData cd=new ConfigData(code,name);
- data.add(cd);
- }
- datas.put(type, data);
- }
- }catch(Exception e){
- throw new KhException("无法解析xml配置文件");
- }
- }
- public Map getDatas() {
- return datas;
- }
- public void setDatas(Map datas) {
- this.datas = datas;
- }
- public static void main(String args[]){
- XmlUtil xml=new XmlUtil("/com/zhengxin/eoms/check/config/checkData.xml");
- Map map=xml.getDatas();
- List list=(List) map.get("lineRank");
- for(int i=0;i<list.size();i++){
- ConfigData cd=(ConfigData) list.get(i);
- System.out.println("-------------->Main: code="+cd.getCode()+" name="+cd.getName());
- }
- }
- }
- package com.zhengxin.eoms.check.common.util;
- public class ConfigData {
- private String code;
- private String name;
- public ConfigData(String code, String name) {
- this.code = code;
- this.name = name;
- }
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- <?xml version="1.0" encoding="GBK"?>
- <dictionary>
- <dic type="lineRank">
- <word code="1">一干</word>
- <word code="2">二干</word>
- <word code="3">本地网</word>
- </dic>
- <dic type="lineNature">
- <word code="4">直埋</word>
- <word code="5">架空</word>
- <word code="6">管道</word>
- </dic>
- <dic type="errorNature">
- <word code="7">外力影响</word>
- <word code="8">自然灾害</word>
- <word code="9">人为破坏</word>
- <word code="10">车挂车撞</word>
- <word code="11">其他</word>
- </dic>
- <dic type="cutType">
- <word code="12">中断</word>
- <word code="13">纵剖</word>
- <word code="14">调纤</word>
- </dic>
- <dic type="cutReason">
- <word code="15">现网调整</word>
- <word code="16">线路改造</word>
- <word code="17">外力影响</word>
- <word code="18">其他</word>
- </dic>
- </dictionary>