当前位置:网站首页>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("=====================");
}
}
}
效果图如下:
边栏推荐
- How to avoid arc generation—— Aafd fault arc detector solves the problem for you
- 同事的接口文档我每次看着就头大,毛病多多。。。
- [monitoring] ZABBIX
- Font design symbol combination multifunctional wechat applet source code
- 如何有效对直流列头柜进行监测
- [binary tree] the maximum difference between a node and its ancestor
- Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
- 香港珠宝大亨,22亿“抄底”佐丹奴
- 取得PMP证书需要多长时间?
- [Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
猜你喜欢
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
「运维有小邓」域密码策略强化器
uniapp 除了数字,其他输入无效
French scholars: the explicability of counter attack under optimal transmission theory
Jar批量管理小工具
Why does infographic help your SEO
45 year old professor, she threw two super unicorns
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
[IELTS reading] Wang Xiwei reading P3 (heading)
随机推荐
A mining of edu certificate station
45岁教授,她投出2个超级独角兽
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
MIT-6.824-lab4B-2022(万字思路讲解-代码构建)
如何有效对直流列头柜进行监测
Redis: redis message publishing and subscription (understand)
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
华泰证券低佣金的开户链接安全吗?
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Etcd database source code analysis - brief process of processing entry records
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
PMP证书续证流程
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Examples of time (calculation) total tools: start time and end time of this year, etc
The input of uniapp is invalid except for numbers
Chinese verification of JS regular expressions (turn)
The difference between debug and release
[ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
基于三维gis平台的消防系统运用
Significance of acrel EMS integrated energy efficiency platform in campus construction