当前位置:网站首页>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("=====================");
}
}
}
效果图如下:
边栏推荐
- 取得PMP證書需要多長時間?
- Solve the problem that the virtual machine cannot be remotely connected through SSH service
- 城市轨道交通站应急照明疏散指示系统设计
- Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
- PMP证书续证流程
- 跨域请求
- phpcms付费阅读功能支付宝支付
- Solution record of jamming when using CAD to move bricks in high configuration notebook
- Significance of acrel EMS integrated energy efficiency platform in campus construction
- Acrel-EMS综合能效平台在校园建设的意义
猜你喜欢
PMP certificate renewal process
【雅思阅读】王希伟阅读P4(matching1)
Hash table, hash function, bloom filter, consistency hash
Significance of acrel EMS integrated energy efficiency platform in campus construction
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Jar批量管理小工具
In the enterprise, win10 turns on BitLocker to lock the disk, how to back up the system, how to recover when the system has problems, and how to recover quickly while taking into account system securi
How long does it take to obtain a PMP certificate?
使用快解析搭建自己的minecraft服务器
如何在外地外网电脑远程公司项目?
随机推荐
Illustrated network: what is gateway load balancing protocol GLBP?
CTF competition problem solution STM32 reverse introduction
「运维有小邓」域密码策略强化器
phpcms付费阅读功能支付宝支付
Question brushing guide public
JS convert pseudo array to array
The pit of sizeof operator in C language
一次edu证书站的挖掘
[IELTS reading] Wang Xiwei reading P3 (heading)
IELTS examination process, what to pay attention to and how to review?
Selected cutting-edge technical articles of Bi Ren Academy of science and technology
Fast analysis -- easy to use intranet security software
How to use fast parsing to make IOT cloud platform
Application of machine learning in housing price prediction
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
Mysql database backup and recovery -- mysqldump command
如何有效对直流列头柜进行监测
After Microsoft disables the IE browser, open the IE browser to flash back the solution
Ffmpeg quick clip
【爬虫】数据提取之xpath