当前位置:网站首页>XML解析
XML解析
2022-07-05 11:30:00 【奈良森屿】
目录
dom4j(jar架包)的使用以及使用常用方法获取xml文件中元素和属性
2.1.selectNodes(获取多个元素,小编用来获取根元素)
一:解析思维导图:
二:首先你需要配置好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标签:可以包含0~N个action标签 --> <config> <!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 --> <action path="/regAction" type="test.RegAction"> <!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串 redirect:只能是false|true,允许空,默认值为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>
三:获取java中配置文件的三种配置位置以及读取方式:
1.在同包的情况下:
Demo1.class.getResourceAsStream("config.xml");2.根路径的情况下:
Demo1.class.getResourceAsStream("/config.xml");根路径的查看方法:
右击项目之后,点击下图中蓝色区域:
进入下图界面,选择Source就可以看到resources和src时同一目录下,小编将config文件放在了resource目录下,所以可以使用根目录的方法获取文件
3.WIN-INF安全路径:
context.getResourceAsStream("/WEB-INF/config");
dom4j(jar架包)的使用以及使用常用方法获取xml文件中元素和属性

1.架包获取
所用jar包 dom4j
架包下载
xml解析架包 密码:hlx8
2.常用方法
2.1.selectNodes(获取多个元素,小编用来获取根元素)
//获取根元素 Element rootElement = doc.getRootElement();2.2.selectSingleNode(获取单个元素)
//获取单个元素 Element rootElement = doc.selectSingleNode();2.3.attributeValue(获取元素中的属性)
String path = action.attributeValue("path"); String type = action.attributeValue("type");2.4.getText(获取属性中的值)
String path = action.getText();3.源代码:
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 { //通过流的方式,获取文件 InputStream in = XmlRead.class.getResourceAsStream("/config.xml"); //读取xml文件,并且帮我们进行解析 SAXReader reader = new SAXReader(); //读取之后放到doc中 Document doc = reader.read(in); //获取根元素 Element rootElement = doc.getRootElement(); List<Element> actions = rootElement.selectNodes("/config/action"); //循环遍历获取action下的属性 for (Element action : actions) { String path = action.attributeValue("path"); String type = action.attributeValue("type"); List<Element> forwards = action.selectNodes("forward"); //循环遍历获取forward下的属性 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("================="); } } }
各位老板,麻烦点点赞,感谢阅读
边栏推荐
- MySQL statistical skills: on duplicate key update usage
- Unity xlua monoproxy mono proxy class
- How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
- Prevent browser backward operation
- COMSOL--三维图形的建立
- 871. Minimum Number of Refueling Stops
- Ffmpeg calls avformat_ open_ Error -22 returned during input (invalid argument)
- How did the situation that NFT trading market mainly uses eth standard for trading come into being?
- The ninth Operation Committee meeting of dragon lizard community was successfully held
- 我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
猜你喜欢

Stop saying that microservices can solve all problems!

AutoCAD -- mask command, how to use CAD to locally enlarge drawings

Three paradigms of database

In the last process before the use of the risk control model, 80% of children's shoes are trampled here

Huawei equipment configures channel switching services without interruption

comsol--三维图形随便画----回转

idea设置打开文件窗口个数
![[crawler] bugs encountered by wasm](/img/29/6782bda4c149b7b2b334238936e211.png)
[crawler] bugs encountered by wasm

【Oracle】使用DataGrip连接Oracle数据库

Ddrx addressing principle
随机推荐
Basics - rest style development
R3Live系列学习(四)R2Live源码阅读(2)
COMSOL -- establishment of 3D graphics
PHP中Array的hash函数实现
C#实现WinForm DataGridView控件支持叠加数据绑定
Deepfake tutorial
Evolution of multi-objective sorting model for classified tab commodity flow
COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
Install esxi 6.0 interactively
Detailed explanation of MATLAB cov function
Characteristics and electrical parameters of DDR4
[Oracle] use DataGrid to connect to Oracle Database
Three paradigms of database
Codeforces Round #804 (Div. 2)
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
How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
pytorch训练进程被中断了
紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
How does redis implement multiple zones?


