当前位置:网站首页>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 !
边栏推荐
猜你喜欢

Qt - - Packaging Programs - - Don't install Qt - can run directly

第七章 行为级建模

Understanding of prior probability, posterior probability and Bayesian formula

Probability distribution

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

The solution of frame dropping problem in gnuradio OFDM operation

Why does the updated DNS record not take effect?

Understanding of expectation, variance, covariance and correlation coefficient

Understanding of maximum likelihood estimation

Matlab code about cosine similarity
随机推荐
STM32GPIO口的工作原理
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
5. Contrôle discret et contrôle continu
Introduction to grpc for cloud native application development
How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
powerbuilder 中使用线程的方法
Js中forEach map无法跳出循环问题以及forEach会不会修改原数组
不算不知道,花呗分期的真实利率居然这么高
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
DataWorks值班表
After modifying the background of jupyter notebook and adding jupyterthemes, enter 'JT -l' and the error 'JT' is not an internal or external command, nor a runnable program
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
Tapdata 的 2.0 版 ,開源的 Live Data Platform 現已發布
由排行榜实时更新想到的数状数值
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
如何制作企业招聘二维码?
Sword finger offer II 041 Average value of sliding window
【目标跟踪】|atom