1.2.1 src根目录下
类名.class.getResourceAsStream(“/config.properties”);
1.2.2 与读取配置文件的类在同一包
类名.class.getResourceAsStream(“config2.properties”);
1.2.3 WEB-INF(或其子目录下)
ServletContext application = this.getServletContext();
InputStream is =
application.getResourceAsStream(“/WEB-INF/config3.properties”);
之后还要用到一个只是叫SAXReader 需要导入两个jar包 切记包名为org.dom4j
2. dom4j+xpath解析xml文件xpath等同数据库的select语句
document.selectNodes(xpath);//查一组
public static void main(String[] args) throws DocumentException {
InputStream is = Demo.class.getResourceAsStream("/config.xml");
SAXReader sax=new SAXReader();
Document document = sax.read(is);
//获取对应节点的内容
List nodes = document.selectNodes("config/action");
for (Element element : nodes) {
String path = element.attributeValue("path");
String type = element.attributeValue("type");
System.out.println("path="+path+",type="+type);
//获取action里面的所有元素
List forwardElement = element.selectNodes("forward");
for (Element ele : forwardElement) {
String name = ele.attributeValue("name");
String forwardPath = ele.attributeValue("path");
String redirect = ele.attributeValue("redirect");
System.out.println("tname="+name+"path="+forwardPath+"redirect="+redirect);
}
}
}
document.selectSingleNode(xpath);//查单个
public static void main1(String[] args) throws DocumentException {
InputStream is = Demo.class.getResourceAsStream("/config.xml");
SAXReader sax=new SAXReader();
Document document = sax.read(is);
//获取对应节点的内容
//List nodes = document.selectNodes("config/action");
Element el =(Element) document.selectSingleNode("config/action");
System.out.println(el.attributeValue("path"));
}
DOM由节点组成
Node
元素节点
属性节点
文本节点



