java读取xml配置⽂件
1.配置⽂件的好处, xml配置⽂件的优点, 当改变底层配置时不需要改变和重新编译代码,只需要在xml中更改就可以了
2.先看下项⽬架构
3.static的作⽤
static表⽰“全局”或者“静态”的意思,⽤来修饰成员变量和成员⽅法
可⽤类名直接访问(⽅便),当然也可以通过对象来访问(但是这是不推荐的)
static final⽤来修饰成员变量和成员⽅法,可简单理解为“全局常量”!
对于变量,表⽰⼀旦给值就不可修改,并且通过类名可以访问。
胡慧中照片
对于⽅法,表⽰不可覆盖,并且可以通过类名直接访问。
//返回符合表达式的节点LIST
List list = document.selectNodes(XPathExpression);
//返回符合表达式的⼀个节点
Node node = document.selectSingleNode(XPathExpression);
dul.municipal.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.mail.Flags.Flag;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
咸菜炒肉
import org.dom4j.io.SAXReader;
import municipl.wkh.*;
//F:/javaworkspace/.metadata/.lipse./tmp0/wtpwebapps/conserve/WEB-INF/classes/templates /***
*  养护项⽬⼯具类
*
*/
public class MaintainUtil {
public static  Map<String, String> cateMap = new HashMap<String, String>();
public static  Map<String, String> typeMap = new HashMap<String, String>();
public static  Map<String, String> subTypeMap = new HashMap<String, String>();
/***
* 初始化养护编码Map读取配置文件失败
*/
static{
read();
}
/***
* 读取养护编码配置⽂件
*/
private static void read()  {
try {
// 使⽤SAXReader需要导⼊dom4j-full.jar包。
/
/ dom4j是⼀个Java的XML API,类似于jdom,⽤来读写XML⽂件的。dom4j是⼀个⾮常⾮常优秀的Java XML  //API,具有功能强⼤和极端易⽤使⽤的特点,同时            SAXReader reader = new SAXReader();
//获取路径
String path = getPath();
//读取⽂件流
InputStream in = new FileInputStream(new File(path));
//⽣成⽂档对应实体
Document doc = ad(in);
//打断点跟踪
Element root = RootElement();
readNodeMap(root,"", false,"cateCode",cateMap);
readNodeMap(root, "",false,"typeCode",typeMap);
如何选购液晶电视
readNodeMap(root, "",true,"subTypeCode",subTypeMap);
} catch (Exception e) {
e.printStackTrace();
}
}
/***
* 递归获取养护编码的值存⼊map中
* @param root
* @param prefix
*/
/
/跟下看看是如何到各⾃归属的
@SuppressWarnings("unchecked")
private static void readNodeMap(Element root, String prefix, Boolean hasStateCode,String flag,Map<String, String> map) {
if (root == null) return;
// 获取属性
List<Attribute> attrs = root.attributes();
if (attrs != null && attrs.size() > 0) {
if(flag.(0).getName())){
if(hasStateCode){
map.(0).getValue(), (1).getValue());
}else {
map.(0).getValue(), (0).getValue());周蕙 我是歌手
}
}
}
// 获取他的⼦节点
List<Element> childNodes = root.elements();
prefix += "\t";
for (Element e : childNodes) {
readNodeMap(e, prefix,hasStateCode,flag,map);
}
}
* @return
*/
//跟下通过什么获取到路径的
private static String getPath() {
//Resource("/") 返回的是classpath的位置
//(Resource("/")); // Class⽂件所在路径
String rootPath = Resource("/").getPath() + "templates";        String fileName = "";
rootPath = rootPath.substring(1, rootPath.length());
时间刺客艾克出装
File file = new File(rootPath);
// 2、获取该⽂件下所有⽂件只获取⽂件名以xml结尾的⽂件
if (ists()) {
String[] filelist = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
dsWith("xml");
}
});
for (String obj : filelist) {
if ("l".equals(obj)) {
fileName = obj;
}
}
}
return rootPath + "/" + fileName;
}
}