当前位置:网站首页>XML parsing
XML parsing
2022-07-05 11:31:00 【Nara Senyu】
Catalog
Two : First you need to configure xml The format of the document
1. In the case of the same package :
dom4j(jar Rack bag ) And use common methods to obtain xml Elements and attributes in the file
2.1.selectNodes( Get multiple elements , Xiaobian is used to get the root element )
2.2.selectSingleNode( Get a single element )
Dear bosses , Please praise it a little , Thank you for reading
One : Analytic mind map :
Two : First you need to configure xml The format of the document
The code is as follows :
<?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 label : Can contain 0~N individual action label --> <config> <!-- action label : Can be full of 0~N individual forward label path: With / Starting string , And the value must be unique Non empty type: character string , Non empty --> <action path="/regAction" type="test.RegAction"> <!-- forward label : No sub tags ; name: character string , same action Label under forward label name The value can't be the same ; path: With / Starting string redirect: Can only be false|true, Allow space , The default value is false --> <forward name="failed" path="/reg.jsp" redirect="false" /> <forward name="success" path="/login.jsp" redirect="true" /> </action> <action path="/loginAction" type="test.LoginAction"> <forward name="failed" path="/login.jsp" redirect="false" /> <forward name="success" path="/main.jsp" redirect="true" /> </action> </config>
3、 ... and : obtain java Three configuration locations and reading methods of configuration files in :
1. In the case of the same package :
Demo1.class.getResourceAsStream("config.xml");2. In the case of root path :
Demo1.class.getResourceAsStream("/config.xml");How to view the root path :
Right click on the item , Click the blue area in the figure below :
Enter the following interface , choice Source You can see that resources and src Under the same directory , Xiaobian will config The papers are in resource Under the table of contents , So you can use the root directory method to get files
3.WIN-INF Safe path :
context.getResourceAsStream("/WEB-INF/config");
dom4j(jar Rack bag ) And use common methods to obtain xml Elements and attributes in the file

1. Shelf bag acquisition
used jar package dom4j
Rack package download
xml Analyze rack package password :hlx8
2. Common methods
2.1.selectNodes( Get multiple elements , Xiaobian is used to get the root element )
// Get the root element Element rootElement = doc.getRootElement();2.2.selectSingleNode( Get a single element )
// Get a single element Element rootElement = doc.selectSingleNode();2.3.attributeValue( Get the attributes in the element )
String path = action.attributeValue("path"); String type = action.attributeValue("type");2.4.getText( Get the value in the attribute )
String path = action.getText();3. Source code :
package com.zking.mvc; import java.io.InputStream; 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 { // By way of flow , get files InputStream in = XmlRead.class.getResourceAsStream("/config.xml"); // Read xml file , And help us analyze SAXReader reader = new SAXReader(); // After reading, put it in doc in Document doc = reader.read(in); // Get the root element Element rootElement = doc.getRootElement(); List<Element> actions = rootElement.selectNodes("/config/action"); // Loop through to get action Properties under for (Element action : actions) { String path = action.attributeValue("path"); String type = action.attributeValue("type"); List<Element> forwards = action.selectNodes("forward"); // Loop through to get forward Properties under for (Element forward : forwards) { String name = forward.attributeValue("name"); String fpath = forward.attributeValue("path"); String redirect = forward.attributeValue("redirect"); System.out.println("name = " + name); System.out.println("fpath = " + fpath); System.out.println("redirect = " + redirect); } System.out.println("path = " + path); System.out.println("type = " + type); System.out.println("================="); } } }
Dear bosses , Please praise it a little , Thank you for reading
边栏推荐
- Harbor镜像仓库搭建
- PHP中Array的hash函数实现
- How to understand super browser? What scenarios can it be used in? What brands are there?
- idea设置打开文件窗口个数
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- Guys, I tested three threads to write to three MySQL tables at the same time. Each thread writes 100000 pieces of data respectively, using F
- DDR4硬件原理图设计详解
- TSQL – identity column, guid, sequence
- Detailed explanation of DDR4 hardware schematic design
- Ffmpeg calls avformat_ open_ Error -22 returned during input (invalid argument)
猜你喜欢

Three suggestions for purchasing small spacing LED display

go语言学习笔记-分析第一个程序

COMSOL -- 3D casual painting -- sweeping
![[Oracle] use DataGrid to connect to Oracle Database](/img/4f/886378667889f730eaed39b97f0a39.png)
[Oracle] use DataGrid to connect to Oracle Database

Harbor镜像仓库搭建

pytorch训练进程被中断了

不要再说微服务可以解决一切问题了!

R3live series learning (IV) r2live source code reading (2)

Codeforces Round #804 (Div. 2)

COMSOL -- establishment of 3D graphics
随机推荐
Detailed explanation of MATLAB cov function
shell脚本文件遍历 str转数组 字符串拼接
Modulenotfounderror: no module named 'scratch' ultimate solution
Redis如何实现多可用区?
基于OpenHarmony的智能金属探测器
7.2每日学习4
Project summary notes series wstax kt session2 code analysis
SLAM 01. Modeling of human recognition Environment & path
POJ 3176-Cow Bowling(DP||记忆化搜索)
Go language learning notes - first acquaintance with go language
阻止瀏覽器後退操作
NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Cron expression (seven subexpressions)
[advertising system] incremental training & feature access / feature elimination
Codeforces Round #804 (Div. 2)
基于Lucene3.5.0怎样从TokenStream获得Token
2048 game logic
基础篇——REST风格开发
程序员内卷和保持行业竞争力


