当前位置:网站首页>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("=====================");
}
}
}
效果图如下:

边栏推荐
- Basic use and upgrade of Android native database
- How to avoid arc generation—— Aafd fault arc detector solves the problem for you
- Jar batch management gadget
- 业务实现-日志写到同一个行数据里面
- The difference between cout/cerr/clog
- The pit of sizeof operator in C language
- CTF競賽題解之stm32逆向入門
- QT personal learning summary
- 法国学者:最优传输理论下对抗攻击可解释性探讨
- 香港珠宝大亨,22亿“抄底”佐丹奴
猜你喜欢

圖解網絡:什麼是網關負載均衡協議GLBP?

French scholars: the explicability of counter attack under optimal transmission theory

青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区

Phpcms paid reading function Alipay payment

Selected cutting-edge technical articles of Bi Ren Academy of science and technology

【雅思阅读】王希伟阅读P4(matching1)

雅思考试流程、需要具体注意些什么、怎么复习?

人脸识别5- insight-face-paddle-代码实战笔记

【雅思阅读】王希伟阅读P3(Heading)

ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
随机推荐
Application of multi loop instrument in base station "switching to direct"
Advanced template
Using the uniapp rich text editor
Chinese verification of JS regular expressions (turn)
端口映射和端口转发区别是什么
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
uniapp 除了数字,其他输入无效
Compare two vis in LabVIEW
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
Build your own minecraft server with fast parsing
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
JS 将伪数组转换成数组
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
CTF競賽題解之stm32逆向入門
微服务(Microservice)那点事儿
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Galera cluster of MariaDB - dual active and dual active installation settings
如何将自己的代码作品快速存证,已更好的保护自己劳动成果
Recommended collection: build a cross cloud data warehouse environment, which is particularly dry!
45 year old professor, she threw two super unicorns