xml解析工具类(需要dom4j-1.6.1.jar和jaxen-1.1.1.jar)
编程技术  /  houtizong 发布于 3年前   87
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>
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接