一、学习导图
二、Java中配置文件的三种配置位置及读取方式
1、同包 :Dhm01.class.getResourceAsStream("db.properties");
2、根路径相同 :Dhm01.class.getResourceAsStream("/db.properties");
例:
package parse;
import java.io.InputStream;
import java.util.Properties;
/**
* 配置文件存在位置
* 1、同包
* WeB-INF 安全目录:凡是放在这个目录的文件不能直接被外界访问
* @author T440s
*
*/
public class Dhm01 {
public static void main(String[] args) throws Exception{
//同包 InputStream in=Dhm01.class.getResourceAsStream("db.properties");
// 根路径相同 InputStream in=Dhm01.class.getResourceAsStream("/db.properties");
Properties p=new Properties();
p.load(in);
System.out.println(p.getProperty("uname"));
}
}
3、WeB-INF 安全目录:凡是放在这个目录的文件不能直接被外界访问
间接访问方式:context.getResourceAsStream("/WeB-INF/db.properties");
package parse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/demo")
public class Dhmserverlet extends HttpServlet{
protected void service(HttpServletRequest req,HttpServletResponse resp) throws IOException {
InputStream in=req.getServletContext().getResourceAsStream("/WeB-INF/db.properties");
Properties p=new Properties();
p.load(in);
System.out.println(p.getProperty("uname"));
}
}
三、dom4j的使用
四种常用方法:
selectNodes 获取对应节点对象,返回list
attributeValue 获取指定对象的属性
selectSingleNode 获取对应节点对象
getText
例如:
package parse;
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 Dhm02 {
public static void main(String[] args) throws DocumentException {
InputStream in=Dhm02.class.getResourceAsStream("students.xml");
SAXReader sr=new SAXReader();
Document doc=sr.read(in);
// selectNodes获取对应节点对象,返回list
List<Element> stuEles=doc.selectNodes("/students/student");
for (Element stuEle : stuEles) {
// attributeValue获取指定对象的属性
String sid=stuEle.attributeValue("sid");
if(sid.equals("s003")) {
// selectSingleNode获取对应节点对象
Element a=(Element) stuEle.selectSingleNode("name");
System.out.println(a.getText());
}
}
}
}
四、xpath的使用
语法:
/ 定位路径 @ 属性
案例:
package parse;
import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dhm03 {
public static void main(String[] args) throws DocumentException {
InputStream in=Dhm03.class.getResourceAsStream("students.xml");
SAXReader sr=new SAXReader();
Document doc=sr.read(in);
Element nameEle= (Element) doc.selectSingleNode("/students/student[@sid='s002']/name");
System.out.println(nameEle.getText());
}
}
五、练习
1、获取所有config中type的值
package 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;
/**
* 获取所有config中type的值
* @author T440s
*
*/
public class Dhmzy01 {
public static void main(String[] args) throws DocumentException {
InputStream in=Dhmzy01.class.getResourceAsStream("config.xml");
SAXReader sr=new SAXReader();
Document doc=sr.read(in);
List<Element> stuEles=doc.selectNodes("/config/action");
for (Element ele : stuEles) {
String type=ele.attributeValue("type");
System.out.println(type);
}
}
}
2、获取第二个config中type的值
package 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;
/**
* 获取第二个config中type的值
* @author T440s
*
*/
public class Dhmzy02 {
public static void main(String[] args) throws DocumentException {
InputStream in=Dhmzy02.class.getResourceAsStream("config.xml");
SAXReader sr=new SAXReader();
Document doc=sr.read(in);
List<Element> stuEles=doc.selectNodes("/config/action");
Element ele=stuEles.get(1);
String type=ele.attributeValue("type");
System.out.println(type);
}
}
3、获取第二个config中所有forward的path
package 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;
/**
* 获取第二个config中所有forward的path
* @author T440s
*
*/
public class Dhmzy03 {
public static void main(String[] args) throws DocumentException {
InputStream in=Dhmzy03.class.getResourceAsStream("config.xml");
SAXReader sr=new SAXReader();
Document doc=sr.read(in);
List<Element> list= doc.selectNodes("/config/action[@path='/loginAction']/forward");
for (Element element : list) {
String path=element.attributeValue("path");
System.out.println(path);
}
}
}
4、获取第二个config中第二个forward的path
package 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;
/**
* 获取第二个config中第二个forward的path
* @author T440s
*
*/
public class Dhmzy04 {
public static void main(String[] args) throws DocumentException {
InputStream in=Dhmzy03.class.getResourceAsStream("config.xml");
SAXReader sr=new SAXReader();
Document doc=sr.read(in);
List<Element> list= doc.selectNodes("/config/action[@path='/loginAction']/forward");
Element ele=list.get(1);
String path=ele.attributeValue("path");
System.out.println(path);
}