码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • XML配置文件的解析


    XML配置文件的解析

    • 解析需要知道的部分知识
      • 获得不同存放路径下XML文件的方法
      • dom4j常用方法
      • xpath语法
    • 案例

    解析需要知道的部分知识

    获得不同存放路径下XML文件的方法

    类名.class.getResourceAsStream(“xxx”):拿到同包下的文件

    类名.class.getResourceAsStream(“/xxx”):拿到根目录下的文件

    context.getResourceAsStream(“/WIN-INF/xxx”):拿到WIN-INF安全路径

    dom4j常用方法

    selectNodes:拿到多个元素

    selectSingleNode:拿到单个元素

    getRootElement():拿到根元素

    attributeValue:只有元素才可以点出这个方法来获取值

    getText:拿到元素文本

    xpath语法

    /:定位路径

    @:属性

    案例

    创建xml文件config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE config[
    	<!ELEMENT config (action*)>
    	<!ELEMENT action (forward*)>
    	<!ELEMENT forward EMPTY>
    	<!ATTLIST action
    	  path CDATA #REQUIRED
    	  type CDATA #REQUIRED
    	>
    	<!ATTLIST forward
    	  name CDATA #REQUIRED
    	  path CDATA #REQUIRED
    	  redirect (true|false) "false"
    	>
    ]>
    <config>
    	<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
    		<forward name="students" path="/students/studentList.jsp" redirect="false"/>
    	</action>
    	<action path="/studentAction02" type="org.lisen.mvc.action.StudentAction">
    		<forward name="students02" path="/students/studentList.jsp" redirect="false"/>
    	</action>
    </config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    导入需要使用的jar包
    在这里插入图片描述

    编写执行代码

    package com.demo;
    
    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 xmlRead {
    
    	public static void main(String[] args) throws Exception {
    		InputStream in = xmlRead.class.getResourceAsStream("/config.xml");
    		SAXReader reader = new SAXReader();
    		Document doc = reader.read(in);
    		
    		Element rootElement = doc.getRootElement();
    		List<Element> action = rootElement.selectNodes("action");
    		
    		for (Element e : action) {
    			String path = e.attributeValue("path");
    			String type = e.attributeValue("type");
    			
    			System.out.println("action path="+path);
    			System.out.println("action type="+type);
    			
    			List<Element> forward = e.selectNodes("forward");
    			for (Element fe : forward) {
    				String fpath = fe.attributeValue("path");
    				String name = fe.attributeValue("name");
    				String redirect = fe.attributeValue("redirect");
    				
    				System.out.println("forward path="+fpath);
    				System.out.println("forward name="+name);
    				System.out.println("forward redirect="+redirect);
    			}
    			System.out.println("==========================");
    		}
    		System.out.println("已完成解析");
    	}
    	
    }
    
    
    • 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

    输出结果:
    在这里插入图片描述

  • 相关阅读:
    k8s-实战——redis集群部署
    网站文章生成技术-网站文章生成工具免费
    java毕业设计公司日常考勤系统mybatis+源码+调试部署+系统+数据库+lw
    2022河南萌新联赛第(七)场:南阳理工学院 F-数对
    第一个2DGodot游戏-从零开始-逐步解析
    myssql基于Spring Boot的宠物猫店管理系统的设计与实现毕业设计源码140909
    Java项目:JSP实现的图书管理系统
    5个网络问题,了解网络协议栈的哪些不为人知的八股文(来波千人福利)
    云渲染的“公”“私”技术!
    Ubuntu环境配置(instant-ngp)
  • 原文地址:https://blog.csdn.net/qq_62217605/article/details/125615150
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号