当前位置:网站首页>Parsing of XML
Parsing of XML
2022-07-04 23:55:00 【Timely】
Catalog
One 、Java Three configuration locations and reading methods of configuration files in
One 、Java Three configuration locations and reading methods of configuration files in
Same bag
InputStream in = XmlReader.class.getResourceAsStream("config.xml");
The root path
InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
WIN-INF Safe path
InputStream in = XmlReader.class.getResourceAsStream("WIN-INF/config.xml");
Two 、XML Analytical way
xml There are four parsing methods :DOM analysis 、SAX analysis 、DOM4J analysis 、JDOM analysis . Today I will focus on DOM4J
What is? DOM4J?
dom4j Is a simple open source library , Used for processing XML、 XPath and XSLT, It's based on Java platform , Use Java The collection framework of , Fully integrated DOM,SAX and JAXP. Download path :
Yes jar Package resources
There is also a code demonstration to teach you how to operate
DOM4J Common methods
Method | describe |
selectNodes | Get multiple nodes |
selectSingleNode | Get a single node |
attributeValue | Returns the specified property value , If the property does not exist , Returns an empty string |
getText | Get the element text |
getRootElemnent | Get the root element |
XPath Use
/ Define the path
@ attribute
SelectNodes("/root/item/@name") take item Of name attribute
Case study :
needed jar package
xml The configuration file 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
path CDATA #REQUIRED
name 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 , The path corresponding to the sub controller
type: character string , Non empty , The full class name of the sub controller
-->
<action path="/registerAction" type="test.action.RegisterAction">
<forward name="success" path="/index.jsp" redirect="true" />
<forward name="failed" path="/register.jsp" redirect="false" />
</action>
<action path="/loginAction" type="test.action.LoginAction">
<forward name="a" path="/index.jsp" redirect="false" />
<forward name="b" path="/welcome.jsp" redirect="true" />
</action>
</config>
xml Parsing code :
package com.zking.demo;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlReader {
public static void main(String[] args)throws Exception {
InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
SAXReader sax = new SAXReader();
Document doc = sax.read(in);
// Get the root element
Element rootElement = doc.getRootElement();
List<Element> actions = rootElement.selectNodes("action");
for(Element e: actions) {
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);
}
System.out.println("=====================");
}
}
}
The renderings are as follows :
边栏推荐
- [Peking University] tensorflow2.0-1-opening
- 「运维有小邓」域密码策略强化器
- [JS] - [sort related] - Notes
- Hash table, hash function, bloom filter, consistency hash
- QT personal learning summary
- Using fast parsing intranet penetration to realize zero cost self built website
- Chinese verification of JS regular expressions (turn)
- Paddleocr tutorial
- 22-07-02周总结
- Application of multi loop instrument in base station "switching to direct"
猜你喜欢
人脸识别5- insight-face-paddle-代码实战笔记
Phpcms paid reading function Alipay payment
如何在外地外网电脑远程公司项目?
MariaDB's Galera cluster application scenario -- multi master and multi active databases
端口映射和端口转发区别是什么
The input of uniapp is invalid except for numbers
"Xiaodeng" domain password policy enhancer in operation and maintenance
Application of fire fighting system based on 3D GIS platform
XML的解析
45 year old professor, she threw two super unicorns
随机推荐
人脸识别5- insight-face-paddle-代码实战笔记
"Xiaodeng" domain password policy enhancer in operation and maintenance
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
How to effectively monitor the DC column head cabinet
PMP证书续证流程
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
多回路仪表在基站“转改直”方面的应用
取得PMP证书需要多长时间?
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
华泰证券低佣金的开户链接安全吗?
[ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
如何在外地外网电脑远程公司项目?
如何用快解析自制IoT云平台
Solution record of jamming when using CAD to move bricks in high configuration notebook
Acrel-EMS综合能效平台在校园建设的意义
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
[JS] - [sort related] - Notes
XML的解析
Five papers recommended for the new development of convolutional neural network in deep learning