当前位置:网站首页>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("=====================");
}
}
}
效果图如下:
边栏推荐
- Selected cutting-edge technical articles of Bi Ren Academy of science and technology
- Header file duplicate definition problem solving "c1014 error“
- Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
- 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
- Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
- Qualcomm WLAN framework learning (30) -- components supporting dual sta
- PMP证书续证流程
- ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
- Chinese verification of JS regular expressions (turn)
- Servlet+jdbc+mysql simple web exercise
猜你喜欢
[JS] - [sort related] - Notes
使用快解析搭建自己的minecraft服务器
Jar batch management gadget
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Selected cutting-edge technical articles of Bi Ren Academy of science and technology
【雅思阅读】王希伟阅读P3(Heading)
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
The input of uniapp is invalid except for numbers
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
QT addition calculator (simple case)
随机推荐
认识ThreadPoolExecutor
ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果
Cross domain request
Solution record of jamming when using CAD to move bricks in high configuration notebook
基于三维gis平台的消防系统运用
Face recognition 5- insight face padding code practice notes
The difference between cout/cerr/clog
Etcd database source code analysis - brief process of processing entry records
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
Microservice
雅思考试流程、需要具体注意些什么、怎么复习?
【监控】zabbix
List related knowledge points to be sorted out
Galera cluster of MariaDB - dual active and dual active installation settings
Application of fire fighting system based on 3D GIS platform
Pytoch --- use pytoch to realize linknet for semantic segmentation
IELTS examination process, what to pay attention to and how to review?
[Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
The caching feature of docker image and dockerfile
电力运维云平台:开启电力系统“无人值班、少人值守”新模式