当前位置:网站首页>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的hello world
- How does Matplotlib and PIL image integrate and save multiple pictures into one picture
- How to realize batch control? MES system gives you the answer
- Codeforces Round #643 (Div. 2)——B. Young Explorers
- Android 创建的sqlite3数据存放位置
- NPM Internal Split module
- About how USRP sets the sampling frequency below the minimum sampling frequency reached by the hardware
- Anaconda3 download address Tsinghua University open source software mirror station
- 5、離散控制與連續控制
- Frequency probability and Bayesian probability
猜你喜欢
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
Understanding of prior probability, posterior probability and Bayesian formula
2022 safety officer-a certificate free examination questions and safety officer-a certificate mock examination
2、TD+Learning
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
MATLAB R2021b 安装libsvm
COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况---材料的添加
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Different methods for setting headers of different pages in word (the same for footer and page number)
The persistence mode of redis - RDB and AOF persistence mechanisms
随机推荐
COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立
QT build with built-in application framework -- Hello World -- use min GW 32bit
How to realize batch control? MES system gives you the answer
[loss function] entropy / relative entropy / cross entropy
2022 R1 fast opening pressure vessel operation test question bank and R1 fast opening pressure vessel operation free test questions
Codeforces Round #643 (Div. 2)——B. Young Explorers
How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
子矩阵的和
AttributeError: ‘str‘ object has no attribute ‘strftime‘
qt--將程序打包--不要安裝qt-可以直接運行
用户之声 | 对于GBase 8a数据库学习的感悟
break algorithm---刷题map
Deep learning website
regular expression
Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
由排行榜实时更新想到的数状数值
Write a pure handwritten QT Hello World
Anaconda3 tutorial on installing and adding Tsinghua image files