当前位置:网站首页>快速熟知XML解析
快速熟知XML解析
2022-07-07 23:53:00 【xiongᥫᩣ】
目录
一,Java中配置文件的三种配置位置及读取方式
类名.class.getResourceAsStream("xxx"):拿到同包下的文件
类名.class.getResourceAsStream("/xxx"):拿到根目录下的文件
context.getResourceAsStream("/WIN-INF/xxx"):拿到WIN-INF安全路径
二,dome4j常用方法
selectNodes:拿到多个元素
selectSingleNode:拿到单个元素
getRootElement():拿到根元素
attributeValue:只有元素才可以点出这个方法来获取值
getText:拿到元素文本
注:元素可以是节点,但是元素中的属性是节点不是元素
三、xpath语法
/:定位路径
@:属性
四,案例
jar包(有需要可以私信博主哦)
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);
}
}
}
}
输出结果:
由此可以看出我们已经拿到了xml文件中的所有属性及元素了
今天的知识分享就到此为止啦,精彩下期继续!
边栏推荐
- redis的持久化方式-RDB和AOF 两种持久化机制
- 2021-03-06 - play with the application of reflection in the framework
- Basic realization of line graph
- ROS problems (topic types do not match, topic datatype/md5sum not match, MSG XXX have changed. rerun cmake)
- Chapter improvement of clock -- multi-purpose signal modulation generation system based on ambient optical signal detection and custom signal rules
- 2021 Shanghai safety officer C certificate examination registration and analysis of Shanghai safety officer C certificate search
- Frequency probability and Bayesian probability
- QT build with built-in application framework -- Hello World -- use min GW 32bit
- Solve the error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- Multi purpose signal modulation generation system based on environmental optical signal detection and user-defined signal rules
猜你喜欢
2、TD+Learning
qt--將程序打包--不要安裝qt-可以直接運行
5、离散控制与连续控制
2022 operation certificate examination for main principals of hazardous chemical business units and main principals of hazardous chemical business units
Macro definition and multiple parameters
2021 Shanghai safety officer C certificate examination registration and analysis of Shanghai safety officer C certificate search
Basic implementation of pie chart
用户之声 | 对于GBase 8a数据库学习的感悟
写一个纯手写的qt的hello world
About snake equation (5)
随机推荐
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
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
Problems of font legend and time scale display of MATLAB drawing coordinate axis
Working principle of stm32gpio port
break algorithm---刷题map
GBASE观察 | 数据泄露频发 信息系统安全应如何守护
Smart agricultural technology framework
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
qt-使用自带的应用框架建立--hello world--使用min GW 32bit
4、策略學習
On the concept and application of filtering in radar signal processing
Common effects of line chart
Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
Write a pure handwritten QT Hello World
Usage of xcolor color in latex
Matlab code on error analysis (MAE, MAPE, RMSE)
项目经理有必要考NPDP吗?我告诉你答案
5. Contrôle discret et contrôle continu
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list