当前位置:网站首页>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 !
边栏推荐
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- 什么样的MES系统才是好系统
- LaTeX 中 xcolor 颜色的用法
- COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
- 2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
- About snake equation (5)
- common commands
- The beauty of Mathematics -- the principle of fine Fourier transform
- Anaconda3 download address Tsinghua University open source software mirror station
- Matlab code on error analysis (MAE, MAPE, RMSE)
猜你喜欢

Understanding of expectation, variance, covariance and correlation coefficient

Voice of users | understanding of gbase 8A database learning

Graphic network: uncover the principle behind TCP's four waves, combined with the example of boyfriend and girlfriend breaking up, which is easy to understand

从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值

Write a pure handwritten QT Hello World

Matlab code about cosine similarity

云原生应用开发之 gRPC 入门

如何让导电滑环信号更好

2022 safety officer-a certificate free examination questions and safety officer-a certificate mock examination

2、TD+Learning
随机推荐
滑环在直驱电机转子的应用领域
3、多智能体强化学习
ROS problems (topic types do not match, topic datatype/md5sum not match, MSG XXX have changed. rerun cmake)
图解网络:揭开TCP四次挥手背后的原理,结合男女朋友分手的例子,通俗易懂
Scalar / vector / matrix derivation method
qt-使用自带的应用框架建立--hello world--使用min GW 32bit
Chapter 7 behavior level modeling
腾讯游戏客户端开发面试 (Unity + Cocos) 双重轰炸 社招6轮面试
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Coordinate conversion of one-dimensional array and two-dimensional matrix (storage of matrix)
The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
[SolidWorks] modify the drawing format
ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original
Anaconda3 download address Tsinghua University open source software mirror station
body有8px的神秘边距
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings
Working principle of stm32gpio port