当前位置:网站首页>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 can China Africa diamond accessory stones be inlaid to be safe and beautiful?
Advanced technology management - what is the physical, mental and mental strength of managers
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
[Oracle] use DataGrid to connect to Oracle Database
Modulenotfounderror: no module named 'scratch' ultimate solution
idea设置打开文件窗口个数
【爬虫】wasm遇到的bug
无密码身份验证如何保障用户隐私安全?
comsol--三维图形随便画----回转
pytorch训练进程被中断了
随机推荐
shell脚本文件遍历 str转数组 字符串拼接
Three suggestions for purchasing small spacing LED display
Codeforces Round #804 (Div. 2)
Detailed explanation of MATLAB cov function
解决访问国外公共静态资源速度慢的问题
以交互方式安装ESXi 6.0
How did the situation that NFT trading market mainly uses eth standard for trading come into being?
【爬虫】wasm遇到的bug
DDR4的特性与电气参数
Advanced technology management - what is the physical, mental and mental strength of managers
Spark Tuning (I): from HQL to code
无密码身份验证如何保障用户隐私安全?
我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
Home office things community essay
居家办公那些事|社区征文
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
Harbor image warehouse construction
How can edge computing be combined with the Internet of things?
The art of communication III: Listening between people