目录
一、java中配置文件的三种配置读取方式
① 配置文件有啥好处
② 各个地方的位置有啥讲究
二、①dom4j 的使用
②xpath 的使用
三、案例
一、java中配置文件的三种配置读取方式
① 配置文件有啥好处:
(.properties/.xml/.yml)
1.使代码更加灵活
2.维护性更高
② 各个地方的位置有啥讲究:
同一层级:方便同步编码
根目录:一般不做频繁更改
web-inf:安全目录(不能直接访问,需要通过内部程序(Servlet)间接访问)
使用db.properties文件配置数据库连接属性,实现对MySQL数据库的连接
源码:
package com.hemingxiang.parse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Demo1 {
public static void main(String[] args) throws Exception {
//同包
// InputStream in = Demo1.class.getResourceAsStream("db.properties");
//根目录
InputStream in = Demo1.class.getResourceAsStream("/db.properties");
//.properties文件有一个专门类解析
Properties p=new Properties();
//此时p就加载了db.properties中的所有信息
p.load(in);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upass"));
}
}
同一层级运行输出:
二、①dom4j 的使用
selectNodes (获取多个标签)
selectSingleNode (获取单个标签)
attributeValue (获取属性值)
getText (获取标签值)
源码解析:
package com.hemingxiang.parse;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo2 {
public static void main(String[] args) throws Exception {
InputStream in = Demo2.class.getResourceAsStream("students.xml");
SAXReader sr = new SAXReader();
// 将配置文件中的所有内容获取到了
Document doc = sr.read(in);
// selectNodes 根据标签名,返回标签集合
// System.out.println(doc.asXML());
// List studentsEles = doc.selectNodes("students");
// System.out.println(studentsEles.size());
// List studentEles = studentsEles.get(0).selectNodes("student");
// System.out.println(studentEles.size());
// selectNodes 根据标签名,返回单个标签,通常用于已经知晓该标签只会出现一次
Element studentsEle = (Element) doc.selectSingleNode("students");
List studentEles = studentsEle.selectNodes("student");
for (Element stuEle : studentEles) {
System.out.println(stuEle.attributeValue("sid"));// attributeValue是拿到属性值
System.out.println(stuEle.selectSingleNode("name").getText());;// getText拿到标签里的内容
}
}
}
输出结果:
②xpath 的使用
源码解析:
package com.hemingxiang.pares;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Demo3 {
public static void main(String[] args) throws Exception {
// /代表层级,代表查找
// @精确定位
InputStream in = Demo3.class.getResourceAsStream("students.xml");
SAXReader sr = new SAXReader();
//配置文件中所有内容获取到了
Document read = sr.read(in);
// Element stus002 = (Element)read.selectSingleNode("/students/student[@sid='s002']/name");
// System.out.println(stus002.getText());
Element studentsEle = (Element)read.selectSingleNode("students");
List stuEles = studentsEle.selectNodes("student");
for (Element stuEle : stuEles) {
if("s002".equals(stuEle.attributeValue("sid"))) {
System.out.println(stuEle.selectSingleNode("name").getText());
}
}
}
}
输出结果:
三、案例
1)获取所有action中的type值
package com.hemingxiang.pares;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Anli {
public static void main(String[] args) throws Exception {
//根目录
InputStream in = Anli.class.getResourceAsStream("config2.xml");
//读取xml文件
SAXReader sr = new SAXReader();
//获取配置文件中所有内容
Document read = sr.read(in);
Element config = (Element)read.selectSingleNode("config");
List ls= config.selectNodes("action");
//遍历集合
for (Element conf : ls) {
System.out.println(conf.attributeValue("type"));
}
}
}
2)获取第二个action中的type值
package com.hemingxiang.pares;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Anli2 {
public static void main(String[] args) throws Exception {
//根目录
InputStream in = Anli2.class.getResourceAsStream("config2.xml");
SAXReader sr = new SAXReader();
//获取配置文件中所有内容
Document read = sr.read(in);
Element config = (Element)read.selectSingleNode("/config/action[@path='/loginAction']");
System.out.println(config.attributeValue("type"));
}
}
3)获取第二个action的所有forward的path
package com.hemingxiang.pares;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Anli3{
public static void main(String[] args) throws Exception {
//根目录
InputStream in = Anli3.class.getResourceAsStream("config2.xml");
SAXReader sr = new SAXReader();
//获取配置文件中所有内容
Document read = sr.read(in);
Element config = (Element)read.selectSingleNode("/config/action[@path='/loginAction']");
List ls = config.selectNodes("forward");
for (Element conf : ls) {
System.out.println(con.attributeValue("path"));
}
}
}
4)获取第二个action中的第二个forward的path
package com.hemingxiang.pares;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Anli4 {
public static void main(String[] args) throws Exception {
//根目录
InputStream in = Anli4.class.getResourceAsStream("config2.xml");
SAXReader sr = new SAXReader();
//获取配置文件中所有内容
Document read = sr.read(in);
Element config = (Element)read.selectSingleNode("/config/action[@path='/loginAction']");
List ls = config.selectNodes("forward");
for (Element conf : ls) {
if("b".equals(con.attributeValue("name"))) {
System.out.println(con.attributeValue("path"));
}
}
}
}



