zoukankan      html  css  js  c++  java
  • 使用dom4j解析xml文件

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

    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);
                        }
                    }
                }
            }
        }
    }
  • 相关阅读:
    SpringBoot集成Swagger2并配置多个包路径扫描
    SpringBoot基于EasyExcel解析Excel实现文件导出导入、读取写入
    SpringBoot + SpringSecurity + Quartz + Layui实现系统权限控制和定时任务
    Redis数据持久化(RDB、AOF)
    so安全编译选项(栈溢出保护)
    Python调用Chrome下载文件
    AtomicInteger如何保证线程安全以及乐观锁/悲观锁的概念
    SpringCloud:Eureka的健康检测机制
    共享锁(S锁)和排它锁(X锁)
    Collection接口及其常用子类(java.util包)
  • 原文地址:https://www.cnblogs.com/huxiaocong/p/10999905.html
Copyright © 2011-2022 走看看