读取指定文件下的资源文件(db.propeties)
1、读取同包下的资源文件
2、资源文件存放在根目录下
properties.class
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class properties { public static void main(String[] args) throws IOException { //1、读取根目录下文件(db.propeties) InputStream in=properties.class.getResourceAsStream("/db.properties"); //2、读取同包下文件(db.propeties) //InputStream in=properties.class.getResourceAsStream("db.properties"); Properties p=new Properties(); p.load(in); System.out.println(p.getProperty("uname")); System.out.println(p.getProperty("upass")); } } |
3、资源文件存放在web-inf文件(db.propeties)
|
1
|
parseServlet .java |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.zking.properties;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class parseServlet */public class parseServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public parseServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context=request.getServletContext(); InputStream in=context.getResourceAsStream("/WEB-INF/db.properties"); Properties p=new Properties(); p.load(in); System.out.println(p.getProperty("uname")); System.out.println(p.getProperty("upass")); }} |
dom4j + xpase解析XML
|
1
|
XMLDemo .class |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.zking.properties;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;/* * dom4j * jdom jdk * sax解析 * * 解析指定路径下的支援 * */public class XMLDemo {public static void main(String[] args) throws Exception { InputStream in= XMLDemo.class.getResourceAsStream("students.xml"); SAXReader sax=new SAXReader(); Document doc=sax.read(in); System.out.println(doc.asXML());// xpath解析// xpath解析能够将xml格式的串当作目录结构来进行查找// List<Element> list=doc.selectNodes("/students/student");// for (Element element : list) {// if("s002".equals(element.attributeValue("sid"))) {// System.out.println(element.asXML());// Element name=(Element)element.selectSingleNode("name");// System.out.println(name.asXML());// System.out.println(name.getText());// // }// } Element node=(Element) doc.selectSingleNode("/students/student[@sid='s002']"); System.out.println(node.asXML()); } } |
xml解析
Temp.class
1、获取所有action中的type的值
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(String[] args) throws Exception { InputStream in=XmlDemo.class.getResourceAsStream("config.xml"); SAXReader sax= new SAXReader(); Document doc=sax.read(in); // 获取所有action中的type的值 List<Element> stuEles= doc.selectNodes("/config/action"); for (Element stuEle : stuEles) { String type=stuEle.attributeValue("type"); System.out.println(type); } } |
2、获取第二个action中的type的值
|
1
2
3
4
5
6
7
8
9
|
public static void main(String[] args) throws Exception { List<Element> stuEles= doc.selectNodes("/config/action"); for (Element stuEle : stuEles) { if("/loginAction".equals(stuEle.attributeValue("path"))) { String type=stuEle.attributeValue("type"); System.out.println(type); } }} |
3、获取第二个action的所有forward的path
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(String[] args) throws Exception { List<Element> stuEles= doc.selectNodes("/config/action"); for (Element stuEle : stuEles) { if("/loginAction".equals(stuEle.attributeValue("path"))) { List<Element> ford=(List<Element>) stuEle.selectNodes("forward"); for (Element element : ford) { String path=element.attributeValue("path"); System.out.println(path); } } }} |
4、获取第二个action的第二个forward的path
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void main(String[] args) throws Exception { List<Element> stuEles= doc.selectNodes("/config/action"); for (Element stuEle : stuEles) { if("/loginAction".equals(stuEle.attributeValue("path"))) { List<Element> ford=(List<Element>) stuEle.selectNodes("forward"); for (Element element : ford) { if("success".equals(element.attributeValue("name"))) { String path=element.attributeValue("path"); System.out.println(path); } } } } } |
浙公网安备 33010602011771号