当前位置:网站首页>Parsing of XML
Parsing of XML
2022-07-04 23:55:00 【Timely】
Catalog
One 、Java Three configuration locations and reading methods of configuration files in
One 、Java Three configuration locations and reading methods of configuration files in
Same bag
InputStream in = XmlReader.class.getResourceAsStream("config.xml");The root path
InputStream in = XmlReader.class.getResourceAsStream("/config.xml");WIN-INF Safe path
InputStream in = XmlReader.class.getResourceAsStream("WIN-INF/config.xml");Two 、XML Analytical way
xml There are four parsing methods :DOM analysis 、SAX analysis 、DOM4J analysis 、JDOM analysis . Today I will focus on DOM4J
What is? DOM4J?
dom4j Is a simple open source library , Used for processing XML、 XPath and XSLT, It's based on Java platform , Use Java The collection framework of , Fully integrated DOM,SAX and JAXP. Download path :
Yes jar Package resources

There is also a code demonstration to teach you how to operate

DOM4J Common methods
| Method | describe |
| selectNodes | Get multiple nodes |
| selectSingleNode | Get a single node |
| attributeValue | Returns the specified property value , If the property does not exist , Returns an empty string |
| getText | Get the element text |
| getRootElemnent | Get the root element |
XPath Use
/ Define the path
@ attribute
SelectNodes("/root/item/@name") take item Of name attribute
Case study :
needed jar package

xml The configuration file 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 label : Can contain 0~N individual action label
-->
<config>
<!--
action label : Can be full of 0~N individual forward label path: With / Starting string , And the value must be unique Non empty , The path corresponding to the sub controller
type: character string , Non empty , The full class name of the sub controller
-->
<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 Parsing code :
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);
// Get the root element
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("=====================");
}
}
}
The renderings are as follows :

边栏推荐
- The input of uniapp is invalid except for numbers
- Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
- Hash table, hash function, bloom filter, consistency hash
- 快解析——好用的内网安全软件
- 45岁教授,她投出2个超级独角兽
- Significance of acrel EMS integrated energy efficiency platform in campus construction
- Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
- Is the account opening link of Huatai Securities with low commission safe?
- MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
- How to save your code works quickly to better protect your labor achievements
猜你喜欢

How many triangles are there in the golden K-line diagram?

Combien de temps faut - il pour obtenir un certificat PMP?
![[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation](/img/41/eb790e7419a158e985fa503bd7dc17.png)
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation

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

【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)

Phpcms paid reading function Alipay payment

Application of multi loop instrument in base station "switching to direct"

Qualcomm WLAN framework learning (30) -- components supporting dual sta

蓝天NH55系列笔记本内存读写速度奇慢解决过程记录

如何避免电弧产生?—— AAFD故障电弧探测器为您解决
随机推荐
Actual combat simulation │ JWT login authentication
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
S32 design studio for arm 2.2 quick start
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
打新债开户注册安全吗?有没有风险的?靠谱吗?
如何用快解析自制IoT云平台
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
解决无法通过ssh服务远程连接虚拟机
如果炒股开华泰证券的户,在网上开户安全吗?
取得PMP證書需要多長時間?
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)
【监控】zabbix
ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
QT addition calculator (simple case)
【雅思阅读】王希伟阅读P4(matching1)
How to do the project of computer remote company in foreign Internet?
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
城市轨道交通站应急照明疏散指示系统设计