当前位置:网站首页>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
边栏推荐
- How did the situation that NFT trading market mainly uses eth standard for trading come into being?
- 【爬虫】charles unknown错误
- POJ 3176-Cow Bowling(DP||记忆化搜索)
- Detailed explanation of DDR4 hardware schematic design
- I used Kaitian platform to build an urban epidemic prevention policy inquiry system [Kaitian apaas battle]
- idea设置打开文件窗口个数
- [there may be no default font]warning: imagettfbbox() [function.imagettfbbox]: invalid font filename
- Sklearn model sorting
- Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
- [SWT component] content scrolledcomposite
猜你喜欢

高校毕业求职难?“百日千万”网络招聘活动解决你的难题

pytorch训练进程被中断了

7.2 daily study 4

中非 钻石副石怎么镶嵌,才能既安全又好看?

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment

DDR4硬件原理图设计详解

How to introduce devsecops into enterprises?

How to make full-color LED display more energy-saving and environmental protection

如何让全彩LED显示屏更加节能环保

COMSOL -- 3D casual painting -- sweeping
随机推荐
2048 game logic
Huawei equipment configures channel switching services without interruption
[crawler] Charles unknown error
龙蜥社区第九次运营委员会会议顺利召开
Zcmu--1390: queue problem (1)
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
Pytorch training process was interrupted
Oneforall installation and use
如何通俗理解超级浏览器?可以用于哪些场景?有哪些品牌?
Web API configuration custom route
【爬虫】charles unknown错误
《看完就懂系列》15个方法教你玩转字符串
SLAM 01. Modeling of human recognition Environment & path
[LeetCode] Wildcard Matching 外卡匹配
查看多台机器所有进程
Go language learning notes - first acquaintance with go language
MySQL 巨坑:update 更新慎用影响行数做判断!!!
Solve the problem of slow access to foreign public static resources
[leetcode] wild card matching
POJ 3176 cow bowling (DP | memory search)


