java中XML解析

XML解析示例

需要jar包:dom4j-1.6.1.jar和jaxen-1.1.1.jar
Java代码   收藏代码
  1. package com.zhengxin.eoms.check.common.util;  
  2.   
  3. import java.io.InputStream;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.dom4j.Document;  
  11. import org.dom4j.Element;  
  12. import org.dom4j.io.SAXReader;  
  13.   
  14. import com.zhengxin.eoms.check.service.exception.KhException;  
  15. /** 
  16.  * xml配置文件解析类 
  17.  * @author Administrator 
  18.  * 
  19.  */  
  20. public class XmlUtil {  
  21.     private InputStream is;  
  22.     private Map datas=new HashMap();  
  23.       
  24.     public XmlUtil(String configFilePath){  
  25.         if(configFilePath==null){  
  26.             throw new KhException("没有初始化配置文件");  
  27.         }  
  28.         try{  
  29.               
  30.             is=this.getClass().getResourceAsStream(configFilePath);  
  31.           
  32.         }catch(Exception e){  
  33.             throw new KhException("找不到配置文件");  
  34.         }  
  35.         init();   
  36.     }  
  37.       
  38.     private void init(){  
  39.           
  40.         SAXReader sax=new SAXReader();  
  41.         try{  
  42.            Document doc=sax.read(is);  
  43.            List dicList=doc.selectNodes("/dictionary/dic");  
  44.            Iterator it1=dicList.iterator();  
  45.            while(it1.hasNext()){  
  46.                Element dic=(Element) it1.next();  
  47.                String type=dic.attributeValue("type");  
  48.              //  System.out.println("type="+type);  
  49.                List data=new ArrayList();  
  50.                List wordList=dic.elements();  
  51.                Iterator it2=wordList.iterator();  
  52.                while(it2.hasNext()){  
  53.                    Element word=(Element) it2.next();  
  54.                    String code=word.attributeValue("code");  
  55.                    String name=word.getTextTrim();  
  56.                   // System.out.println("code="+code+"  name="+name);  
  57.                    ConfigData cd=new ConfigData(code,name);  
  58.                    data.add(cd);    
  59.                }  
  60.                  
  61.                datas.put(type, data);  
  62.            }  
  63.              
  64.   
  65.         }catch(Exception e){  
  66.             throw new KhException("无法解析xml配置文件");  
  67.         }  
  68.     }  
  69.   
  70.     public Map getDatas() {  
  71.         return datas;  
  72.     }  
  73.   
  74.     public void setDatas(Map datas) {  
  75.         this.datas = datas;  
  76.     }  
  77.       
  78.     public static void main(String args[]){  
  79.         XmlUtil xml=new XmlUtil("/com/zhengxin/eoms/check/config/checkData.xml");  
  80.         Map map=xml.getDatas();  
  81.         List list=(List) map.get("lineRank");  
  82.         for(int i=0;i<list.size();i++){  
  83.             ConfigData cd=(ConfigData) list.get(i);  
  84.             System.out.println("-------------->Main:  code="+cd.getCode()+" name="+cd.getName());  
  85.         }  
  86.     }  
  87.   
  88.       
  89.       
  90.       
  91.       
  92.   
  93. }  




Java代码   收藏代码
  1. package com.zhengxin.eoms.check.common.util;  
  2.   
  3. public class ConfigData {  
  4.       
  5.     private String code;  
  6.       
  7.     private String name;  
  8.   
  9.     public ConfigData(String code, String name) {  
  10.         this.code = code;  
  11.         this.name = name;  
  12.     }  
  13.   
  14.     public String getCode() {  
  15.         return code;  
  16.     }  
  17.   
  18.     public void setCode(String code) {  
  19.         this.code = code;  
  20.     }  
  21.   
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  
  29.       
  30.   
  31. }  



Java代码   收藏代码
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <dictionary>  
  3.   <dic type="lineRank">  
  4.     <word code="1">一干</word>  
  5.     <word code="2">二干</word>  
  6.     <word code="3">本地网</word>  
  7.   </dic>  
  8.   <dic type="lineNature">  
  9.     <word code="4">直埋</word>  
  10.     <word code="5">架空</word>  
  11.     <word code="6">管道</word>  
  12.   </dic>  
  13.   <dic type="errorNature">  
  14.     <word code="7">外力影响</word>  
  15.     <word code="8">自然灾害</word>  
  16.     <word code="9">人为破坏</word>  
  17.     <word code="10">车挂车撞</word>  
  18.     <word code="11">其他</word>  
  19.   </dic>  
  20.   <dic type="cutType">  
  21.     <word code="12">中断</word>  
  22.     <word code="13">纵剖</word>  
  23.     <word code="14">调纤</word>  
  24.   </dic>  
  25.   <dic type="cutReason">  
  26.     <word code="15">现网调整</word>  
  27.     <word code="16">线路改造</word>  
  28.     <word code="17">外力影响</word>  
  29.     <word code="18">其他</word>  
  30.       
  31.   </dic>  
  32. </dictionary>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值