当前位置:网站首页>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 !
边栏推荐
- 2022 safety officer-b certificate examination question bank and safety officer-b certificate simulation test questions
- Dataworks duty table
- 3、多智能体强化学习
- Matlab code on error analysis (MAE, MAPE, RMSE)
- Qt - - Packaging Programs - - Don't install Qt - can run directly
- 用户之声 | 对于GBase 8a数据库学习的感悟
- [loss function] entropy / relative entropy / cross entropy
- Matlab method is good~
- Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
- ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original
猜你喜欢

Android 创建的sqlite3数据存放位置

About snake equation (3)

第七章 行为级建模

break net

Understanding of maximum likelihood estimation

ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版

Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××

From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run

2022 high voltage electrician examination skills and high voltage electrician reexamination examination

5. Contrôle discret et contrôle continu
随机推荐
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
子矩阵的和
什么样的MES系统才是好系统
LaTeX 中 xcolor 颜色的用法
About snake equation (3)
QT -- package the program -- don't install qt- you can run it directly
2022 safety officer-a certificate free examination questions and safety officer-a certificate mock examination
[SolidWorks] modify the drawing format
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
regular expression
腾讯游戏客户端开发面试 (Unity + Cocos) 双重轰炸 社招6轮面试
Sum of submatrix
项目经理有必要考NPDP吗?我告诉你答案
碳刷滑环在发电机中的作用
Why does the updated DNS record not take effect?
批次管控如何实现?MES系统给您答案
PHP to get information such as audio duration
Anaconda3 tutorial on installing and adding Tsinghua image files
The difference between distribution function and probability density function of random variables