当前位置:网站首页>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 !
边栏推荐
- Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
- Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
- Redux usage
- PHP to get information such as audio duration
- ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
- Understanding of expectation, variance, covariance and correlation coefficient
- Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension
- 【SolidWorks】修改工程图格式
- Introduction to natural language processing (NLP) based on transformers
- Understanding of prior probability, posterior probability and Bayesian formula
猜你喜欢
How to make enterprise recruitment QR code?
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Understanding of expectation, variance, covariance and correlation coefficient
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
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
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
图解网络:揭开TCP四次挥手背后的原理,结合男女朋友分手的例子,通俗易懂
About snake equation (3)
Why does the updated DNS record not take effect?
随机推荐
【目标跟踪】|atom
5. Discrete control and continuous control
Different methods for setting headers of different pages in word (the same for footer and page number)
About snake equation (5)
ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
AttributeError: ‘str‘ object has no attribute ‘strftime‘
[loss function] entropy / relative entropy / cross entropy
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
2、TD+Learning
Qt - - Packaging Programs - - Don't install Qt - can run directly
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
Scalar / vector / matrix derivation method
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
LeetCode 练习——剑指 Offer 36. 二叉搜索树与双向链表
QT--创建QT程序
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
5、離散控制與連續控制
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
第七章 行为级建模
3、多智能体强化学习