一.读取指定位置下的资源文件

src根目录下
类名.class.getResourceAsStream("/文件名");

与读取配置文件的类在同一包
类名.class.getResourceAsStream("文件名");

WEB-INF(或其子目录下)

ServletContext servletContext=request.getServletContext();
InputStream ins = servletContext.getResourceAsStream("/WEB-INF/文件名");

二.解析指定路径下的资源文件:

 properties文件的解析方式有java.util.properties这个类来完成(properties文件以键值对的形式存在,需通过键名来获取值)

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiseDemo {

    public static void main(String[] args) throws IOException {
        //获取到同包下的资源文件  将其转化为流对象
        InputStream  ins=PropertiseDemo.class.getResourceAsStream("/db.properties");
        
        //需要专业的工具类来将流中的数据解析出来
        Properties p=new Properties();
        p.load(ins);
        System.out.println("uname");
        System.out.println("upass");
    }
}

 

解析xml文件

xml文件传统解析方式有dom4解析jdk/jdom解析,sax解析

jdk/jdom 和 sax解析方式都是 由上往下解析

dom4j解析是 由外到内解析,需要导包(dom4j-1.6.1.jar,)

由于jdk/jdom和sax解析解析步骤比较复杂,使用的人较少


xpath等同数据库的select语句

document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个

import java.io.File;

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

public class Dome02 {

    public static void main(String[] args) throws Exception {
        // dom4j+path 解析xml文件
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("D:\\eclipse\\j2ee06\\src\\com\\temp\\config.xml"));
        // 查一组
        for (Object o : document.selectNodes("/config/action/forward")) {
            Element element = (Element) o;
            System.out.print(element.attributeValue("mane"));
            System.out.print(element.attributeValue("path"));
            System.out.println(element.attributeValue("redirect"));

        }
        //查询一个
        Element element=(Element)document.selectSingleNode("/config/action");
        System.out.println(element.attributeValue("path"));

    }
}

 列如:

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 Temp {
    public static void main(String[] args) throws Exception {
        InputStream inputStream = Temp.class.getResourceAsStream("/config.xml");
        SAXReader sax = new SAXReader();
        Document document = sax.read(inputStream);
        // 1. 获取所有action中type的值
        List<Element> stuElement = document.selectNodes("/config/action");
        for (Element element : stuElement) {
            String type = element.attributeValue("type");
            System.out.println(type);
        }

        // 2.获取第二个action中的type的值
        for (Element element : stuElement) {
            if ("/loginAction".equals(element.attributeValue("path"))) {
                String type = element.attributeValue("type");
                System.out.println(type);
            }
        }

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

        // 4、获取第二个action的第二个forward的path
        for (Element element : stuElement) {
            if ("/loginAction".equals(element.attributeValue("path"))) {
                List<Element> forward = (List<Element>) element.selectNodes("forward");
                for (Element forwardElement : forward) {
                    if ("success".equals(forwardElement.attributeValue("name"))) {
                        String path = forwardElement.attributeValue("path");
                        System.out.println(path);
                    }
                }
            }
        }
    }
}

 

posted on 2019-06-10 20:07  旧城微冷  阅读(1345)  评论(0编辑  收藏  举报