当前位置:网站首页>Get familiar with XML parsing quickly
Get familiar with XML parsing quickly
2022-07-08 01:44:00 【xiongᥫᩣ】
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
Class name .class.getResourceAsStream("xxx"): Get the documents under the same package
Class name .class.getResourceAsStream("/xxx"): Get the file under the root directory
context.getResourceAsStream("/WIN-INF/xxx"): Get WIN-INF Safe path
Two ,dome4j Common methods
selectNodes: Get multiple elements
selectSingleNode: Get a single element
getRootElement(): Get the root element
attributeValue: Only elements can click this method to get values
getText: Get the element text
notes : Elements can be nodes , But the attribute in the element is a node, not an element
3、 ... and 、xpath grammar
/: Positioning path
@: attribute
Four , Case study
jar package ( If you need it, you can send private messages to bloggers )

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
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<config>
<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
<forward name="students" path="/students/studentList.jsp" redirect="false"/>
</action>
</config>xmlRead.java
package com.zking.xml01;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlRead {
public static void main(String[] args) throws Exception {
InputStream in = XmlRead.class.getResourceAsStream("/config.xml");
SAXReader reader= new SAXReader();
Document doc = reader.read(in);
Element root=doc.getRootElement();
List<Element> action =root.selectNodes("action");
for(Element e: action) {
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);
}
}
}
}Output results :

From this we can see that we have got xml All attributes and elements in the file
That's all for today's knowledge sharing , Wonderful next issue continues !
边栏推荐
- php 获取音频时长等信息
- 【SolidWorks】修改工程图格式
- QT build with built-in application framework -- Hello World -- use min GW 32bit
- Qml 字体使用pixelSize来自适应界面
- 2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
- Introduction to natural language processing (NLP) based on transformers
- 图解网络:揭开TCP四次挥手背后的原理,结合男女朋友分手的例子,通俗易懂
- Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
- 2022 safety officer-a certificate free examination questions and safety officer-a certificate mock examination
- 【目标跟踪】|atom
猜你喜欢

第七章 行为级建模

The difference between distribution function and probability density function of random variables

能力贡献 GBASE三大解决方案入选“金融信创生态实验室-金融信创解决方案(第一批)”

Js中forEach map无法跳出循环问题以及forEach会不会修改原数组

Version 2.0 of tapdata, the open source live data platform, has been released

2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination

子矩阵的和

生态 | 湖仓一体的优选:GBase 8a MPP + XEOS

Gnuradio3.9.4 create OOT module instances

3、多智能体强化学习
随机推荐
regular expression
qt-使用自带的应用框架建立--hello world--使用min GW 32bit
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
能力贡献 GBASE三大解决方案入选“金融信创生态实验室-金融信创解决方案(第一批)”
Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
LeetCode 练习——剑指 Offer 36. 二叉搜索树与双向链表
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立
PHP to get information such as audio duration
Kafka-connect将Kafka数据同步到Mysql
How to make enterprise recruitment QR code?
npm 内部拆分模块
项目经理有必要考NPDP吗?我告诉你答案
Working principle of stm32gpio port
DataWorks值班表
ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
Gbase observation | how to protect the security of information system with frequent data leakage
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
Matlab method is good~