XML解析说明

XML相关技术

1)xml		-> 为框架应用提供数据源
2)dtd		-> 约束xml元素属性组成
3)Schema	-> dtd技术升级(W3C标准)
4)dom4j		-> xml文档解析获取数据 (解析的理论依据是约束规范)

步骤:

1)初始化文档(org.dom4j.Document)对象 -> 文件流加载xml文件
	//获取文件路径
	String path = .......;
	InputStream inputStream = new FileInputStream(path);
	Document document = new SAXReader().read(inputStream);
	inputStream.close();

2)定位节点:XPATH -> XML层次结构
	String xpath = "/节点/节点[@属性='值']"
	1>单节点	Node node = document.selectSingleNode(path);
	2>多节点	List<Node> nodes = document.selectNodes(path);

补充:XML解析

1)SAX解析
	每次重新读取xml文件	-> 效率低 内存消耗低
	
2)DOM解析(推荐) -> 配置不大 内容固定
	加载xml文本并缓存	-> 效率高 内存消费多

练习: 城市.xml

1)列表显示中国所有的省级地区
2)列举自己家乡所有区级地址
3)列举美国所有州级地区

获取城市.xml文件---------------点击这里

百度网盘

链接:https://pan.baidu.com/s/1bSQM3WCaSGVPmp-P0tzCVw 提取码:plab
复制这段内容后打开百度网盘手机App,操作更方便哦

项目文件目录结构图
在这里插入图片描述

代码实现如下:

	@Test
    public void test(){
        String key="user.dir";
        //获得当前程序的运行路径
        String property = System.getProperty(key);
        String path=property+ File.separatorChar+"src"+
                File.separatorChar+"main"+File.separatorChar+"resources"+File.separatorChar+"城市.xml";
        //加载xml文件
        File file=new File(path);
        try {
            InputStream inputStream=new FileInputStream(file);
            Document document=new SAXReader().read(inputStream);
            inputStream.close();
            String xpath="/Location/CountryRegion[@Name='中国']/State";
            List<Node> nodes= document.selectNodes(xpath);
            for(Node node:nodes){
                Element ele = (Element) node;
                System.out.println(ele.attributeValue("Name"));
            }
            System.out.println("----------------------"+nodes.size()+"------------------------");
            xpath="/Location/CountryRegion[@Name='中国']/State[@Name='广东']/City[@Name='河源']/Region";
            nodes= document.selectNodes(xpath);
            for(Node node:nodes){
                Element ele = (Element) node;
                System.out.println(ele.attributeValue("Name"));
            }
            System.out.println("--------------------"+nodes.size()+"--------------------------");
            xpath="/Location/CountryRegion[@Name='美国']/State";
            nodes= document.selectNodes(xpath);
            for(Node node:nodes){
                Element ele = (Element) node;
                System.out.println(ele.attributeValue("Name"));
            }
            System.out.println("--------------------"+nodes.size()+"--------------------------");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

依赖项:pom.xml

	<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你不懂、、、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值