云间录  

XML 和properties

properties:

1、存放于src根目录下

//获取到同包下的资源文件,将其转换成流对象
//InputStream is=	PropertiesDemo.class.getResourceAsStream("/db.properties");

 

//需要专门的工具类来讲流中的数据
Properties p=new Properties();
p.load(is);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upass"));

2、与读取配置文件的类在同一包

//InputStream is= PropertiesDemo.class.getResourceAsStream("db.properties");

Properties p=new Properties();
p.load(is);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upass"));

3、存放在WEB-INF(或其子目录下)

//新建一个servlet类

ServletContext context=request.getServletContext();
InputStream is=context.getResourceAsStream("/WEB-INF/db.properties");
Properties p=new Properties();
p.load(is);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upass"));

 

  

4. dom4j+xpath解析xml文件

InputStream is = XMLDemo.class.getResourceAsStream("students.xml");
//	SAXReader saxReader = new SAXReader();
//	Document doc = saxReader.read(is);
//	System.out.println(doc.asXML());
//	// xpath
//	List<Element> stueles=doc.selectNodes("/students/student");
//	for (Element element : stueles) {
////	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 stuele =(Element) doc.selectSingleNode("/students/student[@sid='s001']");
//	System.out.println(stuele.asXML()); 

  

package com.lingerqi.demo;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 解析指定路径下的资源文件(dom4j)
 * 
 * @author xyls
 *
 */
public class XMLDemo {

	public static void main(String[] args) throws Exception {

		InputStream is = XMlDemo.class.getResourceAsStream("config.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(is);
		List<Element> stueles = doc.selectNodes("/config/action");
		for (Element element : stueles) {
			Element forward = (Element) element.selectSingleNode("forward");
			String type = element.attributeValue("type");
			String path = element.attributeValue("path");
			// if (path.equals("/loginAction")) {
			// 2、获取第二个action中的type的值
			// System.out.println(type);
			// }
			// 1、获取所有action中的type的值
			// System.out.println(type);
			// String fpath=forward.attributeValue("path");

			// System.out.println(type);
			if (element.attributeValue("path").equals("/loginAction")) {
				List<Element> ford = (List<Element>) element.selectNodes("forward");
				for (Element element2 : ford) {
					String fpath = forward.attributeValue("path");
					// 3、获取第二个action的所有forward的path
					System.out.println(fpath);
				}
			}
			if (path.equals("/loginAction")) {
				// 4、获取第二个action的第二个forward的path
				// System.out.println(fpath);
			}
		}
		// if (path.equals("/loginAction")) {
		// 4、获取第二个action的第二个forward的path
		// System.out.println(fpath);
		// }
	}
}

  

 

posted on 2019-06-10 20:32  云间录  阅读(5617)  评论(0编辑  收藏  举报