当前位置:网站首页>XML的解析
XML的解析
2022-07-04 23:47:00 【時宜】
目录
一、Java中配置文件的三种配置位置及读取方式
同包
InputStream in = XmlReader.class.getResourceAsStream("config.xml");根路径
InputStream in = XmlReader.class.getResourceAsStream("/config.xml");WIN-INF安全路径
InputStream in = XmlReader.class.getResourceAsStream("WIN-INF/config.xml");二、XML解析方式
xml解析方式有四种:DOM解析、SAX解析、DOM4J解析、JDOM解析。今天着重讲的是DOM4J
什么是DOM4J?
dom4j 是一个简单的开源库,用于处理 XML、 XPath 和 XSLT,它基于 Java 平台,使用 Java 的集合框架,全面集成了 DOM,SAX 和 JAXP。下载路径:
有jar包资源

还有代码演示教你如何操作

DOM4J常用方法
| 方法 | 描述 |
| selectNodes | 拿到多个节点 |
| selectSingleNode | 拿到单个节点 |
| attributeValue | 返回指定属性值,如果属性不存在,返回空字符串 |
| getText | 拿到元素文本 |
| getRootElemnent | 拿到根元素 |
XPath的使用
/ 定义路径
@ 属性
SelectNodes("/root/item/@name") 取 item 的 name 属性
案例:
需要用到的jar包

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
path CDATA #REQUIRED
name CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<!--
config标签:可以包含0~N个action标签
-->
<config>
<!--
action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 ,子控制器对应的路径
type:字符串,非空,子控制器的完整类名
-->
<action path="/registerAction" type="test.action.RegisterAction">
<forward name="success" path="/index.jsp" redirect="true" />
<forward name="failed" path="/register.jsp" redirect="false" />
</action>
<action path="/loginAction" type="test.action.LoginAction">
<forward name="a" path="/index.jsp" redirect="false" />
<forward name="b" path="/welcome.jsp" redirect="true" />
</action>
</config>xml解析代码:
package com.zking.demo;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlReader {
public static void main(String[] args)throws Exception {
InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
SAXReader sax = new SAXReader();
Document doc = sax.read(in);
//获取根元素
Element rootElement = doc.getRootElement();
List<Element> actions = rootElement.selectNodes("action");
for(Element e: actions) {
String path = e.attributeValue("path");
String type = e.attributeValue("type");
System.out.println("action path = "+path);
System.out.println("action type = "+type);
List<Element> forwards = e.selectNodes("forward");
for(Element f: forwards) {
String name = f.attributeValue("name");
String fpath = f.attributeValue("path");
String redirect = f.attributeValue("redirect");
System.out.println("forward name = "+name);
System.out.println("forward fpath = "+fpath);
System.out.println("forward redirect = "+redirect);
}
System.out.println("=====================");
}
}
}
效果图如下:

边栏推荐
- What is the difference between port mapping and port forwarding
- How to apply for PMP project management certification examination?
- 【北京大学】Tensorflow2.0-1-开篇
- 「运维有小邓」域密码策略强化器
- Redis: redis message publishing and subscription (understand)
- 一次edu证书站的挖掘
- 业务实现-日志写到同一个行数据里面
- uniapp 除了数字,其他输入无效
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- How to save your code works quickly to better protect your labor achievements
猜你喜欢

uniapp 除了数字,其他输入无效

Acrel-EMS综合能效平台在校园建设的意义
![[IELTS reading] Wang Xiwei reading P3 (heading)](/img/19/40564f2afc18fe3e34f218b7b44681.png)
[IELTS reading] Wang Xiwei reading P3 (heading)

Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities

The initial arrangement of particles in SPH (solved by two pictures)

快解析内网穿透帮助企业快速实现协同办公

45 year old professor, she threw two super unicorns

Redis: redis message publishing and subscription (understand)

人脸识别5- insight-face-paddle-代码实战笔记
![[binary tree] the maximum difference between a node and its ancestor](/img/b5/1bc3d102754fc44c6a547807ebab94.png)
[binary tree] the maximum difference between a node and its ancestor
随机推荐
图解网络:什么是网关负载均衡协议GLBP?
[kotlin] the third day
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
Financial markets, asset management and investment funds
【js】-【动态规划】-笔记
Meet ThreadPoolExecutor
Using fast parsing intranet penetration to realize zero cost self built website
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
What is the difference between port mapping and port forwarding
IELTS examination process, what to pay attention to and how to review?
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
[binary tree] the maximum difference between a node and its ancestor
List related knowledge points to be sorted out
45 year old professor, she threw two super unicorns
法国学者:最优传输理论下对抗攻击可解释性探讨
Intelligence test to see idioms guess ancient poems wechat applet source code
【雅思阅读】王希伟阅读P4(matching1)
模板的进阶
C语言中sizeof操作符的坑