当前位置:网站首页>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 :
边栏推荐
- XML的解析
- [论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- Application of multi loop instrument in base station "switching to direct"
- Hash table, hash function, bloom filter, consistency hash
- QT addition calculator (simple case)
- Observable time series data downsampling practice in Prometheus
- 如果炒股开华泰证券的户,在网上开户安全吗?
- Compare two vis in LabVIEW
- 青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
猜你喜欢
Acrel-EMS综合能效平台在校园建设的意义
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
高配笔记本使用CAD搬砖时卡死解决记录
Pytoch --- use pytoch to realize linknet for semantic segmentation
Jar批量管理小工具
How long does it take to obtain a PMP certificate?
Illustrated network: what is gateway load balancing protocol GLBP?
[path planning] RRT adds dynamic model for trajectory planning
圖解網絡:什麼是網關負載均衡協議GLBP?
Combien de temps faut - il pour obtenir un certificat PMP?
随机推荐
股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
认识ThreadPoolExecutor
XML的解析
Meet ThreadPoolExecutor
How to apply for PMP project management certification examination?
Qualcomm WLAN framework learning (30) -- components supporting dual sta
微服务(Microservice)那点事儿
业务实现-日志写到同一个行数据里面
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
[IELTS reading] Wang Xiwei reading P3 (heading)
用快解析内网穿透实现零成本自建网站
Jar批量管理小工具
S32 design studio for arm 2.2 quick start
如何报考PMP项目管理认证考试?
Paddleocr tutorial
js正则表达式之中文验证(转)
Servlet+jdbc+mysql simple web exercise
[IELTS reading] Wang Xiwei reading P4 (matching1)
Nine Qi single chip microcomputer ny8b062d single key control four LED States
Phpcms paid reading function Alipay payment