- Java中配置文件的三种配置位置及读取方式
1.1 XML和*.properties(属性文件)/ini
1.2 存放位置
1.2.1 src根目录下
Xxx.class.getResourceAsStream("/config.properties");
1.2.2 与读取配置文件的类在同一包
Xxx.class.getResourceAsStream(“config2.properties”);
1.2.3 WEB-INF(或其子目录下)、
ServletContext application = this.getServletContext();
InputStream is =
application.getResourceAsStream("/WEB-INF/config3.properties");
2
//通过某个类xxx.class来读取指定的配置文件
InputStream is = Dame.class.getResourceAsStream("/config.xml");
//调用外部架包
SAXReader sa=new SAXReader();
try {
Document read = sa.read(is);
//找到config.xml,action节点
List<Element> selectNodes = read.selectNodes("/config/action");
for (Element n : selectNodes) {
//得到属性的内容
String path = n.attributeValue("path");
String type = n.attributeValue("type");
System.out.println("action:"+path+","+type);
//循环嵌套,根据config.xml,action节点上在添加一个节点
List<Element> selectNodes2 = n.selectNodes("forward");
for (Element s : selectNodes2) {
String name = s.attributeValue("name");
String path1 = s.attributeValue("path");
String redirect = s.attributeValue("redirect");
System.out.println("forward:"+name+","+path1+","+redirect);
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- dom4j+xpath解析xml文件
dom4j是一个外部的工具类
xpath等同数据库的select语句
document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个