有时候咱们会遇到一套管理系统代码部署到不一样省份,不一样省份的又要有不一样的需求,这时候咱们就要考虑在配置文件中使用些参数来控制 代码里面的某些功能的开关java
获取maven项目resource路径dom
//Constants 为这个变量所在类的名称 public static final String resourcePaths =Constants.class.getClassLoader() .getResource("").getPath();
咱们来看看代码是怎么实现的 配置文件里面内容是键值对的形式 因此咱们用一个hashMap来存储单个配置maven
文件里面的内容 有多个配置文件 因此咱们考虑构造一个双重Map code
private static Map<String, Map<String, Object>> confirParamMap = new HashMap<String,Map<String,Object>>();
怎样解析单个XML中的内容到mapxml
<?xml version="1.0" encoding="UTF-8"?> <config> <!--admin门户的地址,绝对地址--> <adminPath>/admin</adminPath> <!-- 企业用户角色ID(已认证) --> <staffIdRoles>7</staffIdRoles> <!--用户角色ID(已认证) --> </config>
咱们用dom4j解析XMLelement
针对上面的XML文件咱们要获取的数据是一个 Element元素组成的集合文档
因此咱们构造这样一个方法 传入参数是一个 List<Element> 返回一个 Map集合部署
private static Map<String, Object> getAllElements(List<Element> childElements, Map<String, Object> mapEle) { for (Element ele : childElements) { mapEle.put(ele.getName(), ele.getText()); if (ele.elements().size() > 0) { mapEle = getAllElements(ele.elements(), mapEle); } } return mapEle; }
读取config.properties下的默认省份get
public class propertyUtil { public static void main(String[] args) { // TODO Auto-generated method stub readProperty(Constants.resourcePaths+"config/config.properties"); } public static Map<String,Object> readProperty(String filePath){ Properties prop = new Properties(); Map<String,Object> map = new HashMap<String,Object>(); try{ //读取属性文件a.properties InputStream in = new BufferedInputStream (new FileInputStream(filePath)); prop.load(in); ///加载属性列表 Iterator<String> it=prop.stringPropertyNames().iterator(); while(it.hasNext()){ String key=it.next(); System.out.println(key+":"+prop.getProperty(key)); map.put(key, prop.getProperty(key)); } in.close(); return map; } catch(Exception e){ return map; } } }
读取function 下配置文件(notifyContent.xml,system.xml) 先读取最外层默认的配置文件 再读取当前省份配置文件,并获取他们的节点string
private static void initMap(String filePath) {// 获取某个目录下的配置文件 File configurefile = new File(filePath); File[] files = configurefile.listFiles(); for (File file : files) {// 目录下面几个XML文件 if (!file.isDirectory()) { String fileName = file.getName(); String[] fileNameList = fileName.split("\\.");//点号须要转义才能分割 SAXReader reader = new SAXReader(); Document document = null; try { document = reader.read(file); } catch (DocumentException e) { // TODO Auto-generated catch block System.out.println("读取文件失败"); e.printStackTrace(); } //读取XML根节点 Element root = document.getRootElement(); // 根节点下面的因此子节点 //也就是 <!--admin门户的地址,绝对地址--> <adminPath>/admin</adminPath> <!-- 企业用户角色ID(已认证) --> <staffIdRoles>7</staffIdRoles> <!--用户角色ID(已认证) --> List<Element> childElements = root.elements(); Map<String, Object> mapEle = new HashMap<String, Object>(); // 遍历子节点 mapEle = getAllElements(childElements, mapEle); if(confirParamMap.containsKey(fileNameList[0])){ //好比如今获取12 目录下面的system.xml 以前已经把外层的system.xml读进去了 for (Entry<String, Object> entry : mapEle.entrySet()) { confirParamMap.get(fileNameList[0]).put(entry.getKey(), entry.getValue()); } }else{ confirParamMap.put(fileNameList[0], mapEle); } } } }
这样咱们在这个类初始化时就调用方法initMap就能将配置文件里面的内容放到双重map里面去了
现附上整个代码
package com.dlh.test; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.dlh.common.util.Constants; public class xmlParseSingleton { private static Map<String, Map<String, Object>> confirParamMap = new HashMap<String, Map<String,Object>>(); public static Map<String, Map<String, Object>> getConfirParamMap() { return confirParamMap; } private xmlParseSingleton() { System.out.println("初始化"); } private static class innerClass { public static final xmlParseSingleton instance = new xmlParseSingleton(); } public static xmlParseSingleton getInstance() { return innerClass.instance; } static { initMap(Constants.resourcePaths + "config/function/"); Map<String,Object> propMap =propertyUtil.readProperty(Constants.resourcePaths+"config/config.properties"); initMap(Constants.resourcePaths + "config/function/"+propMap.get("DEFAULT_PROVINCE")); } // private static void initMap(String filePath) {// 获取某个目录下的配置文件 File configurefile = new File(filePath); File[] files = configurefile.listFiles(); for (File file : files) {// 目录下面几个XML文件 if (!file.isDirectory()) { String fileName = file.getName(); String[] fileNameList = fileName.split("\\.");//点号须要转义才能分割 SAXReader reader = new SAXReader(); Document document = null; try { document = reader.read(file); } catch (DocumentException e) { // TODO Auto-generated catch block System.out.println("读取文件失败"); e.printStackTrace(); } Element root = document.getRootElement(); // 子节点 List<Element> childElements = root.elements(); Map<String, Object> mapEle = new HashMap<String, Object>(); // 遍历子节点 mapEle = getAllElements(childElements, mapEle); if(confirParamMap.containsKey(fileNameList[0])){ //好比如今获取12 目录下面的system.xml 以前已经把外层的system.xml读进去了 for (Entry<String, Object> entry : mapEle.entrySet()) { confirParamMap.get(fileNameList[0]).put(entry.getKey(), entry.getValue()); } }else{ confirParamMap.put(fileNameList[0], mapEle); } } } } private static Map<String, Object> getAllElements(List<Element> childElements, Map<String, Object> mapEle) { for (Element ele : childElements) { /* System.out.println("key:" + ele.getName()); System.out.println("value:" + ele.getText());*/ mapEle.put(ele.getName(), ele.getText()); if (ele.elements().size() > 0) { mapEle = getAllElements(ele.elements(), mapEle); } } return mapEle; } public static void main(String[] args) { System.out.println( confirParamMap.get("system").size()); } }
这里的xml文档的格式比较简单 其实均可以用properties代替