当前位置:网站首页>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 :

边栏推荐
- Compare two vis in LabVIEW
- Basic points of the game setup of the points mall
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- Fast analysis -- easy to use intranet security software
- Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
- PaddleOCR教程
- 如何将自己的代码作品快速存证,已更好的保护自己劳动成果
- Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
- MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
- 【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
猜你喜欢

【雅思阅读】王希伟阅读P3(Heading)

电力运维云平台:开启电力系统“无人值班、少人值守”新模式

OSEK standard ISO_ 17356 summary introduction

How to do the project of computer remote company in foreign Internet?

PMP证书续证流程

It's too convenient. You can complete the code release and approval by nailing it!

Fast parsing intranet penetration helps enterprises quickly achieve collaborative office

How many triangles are there in the golden K-line diagram?

In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database

ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
随机推荐
【监控】zabbix
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
多回路仪表在基站“转改直”方面的应用
[kotlin] the third day
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
如何报考PMP项目管理认证考试?
企业应用业务场景,功能添加和修改C#源码
[IELTS reading] Wang Xiwei reading P4 (matching1)
Observable time series data downsampling practice in Prometheus
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
How to use fast parsing to make IOT cloud platform
Selected cutting-edge technical articles of Bi Ren Academy of science and technology
How many triangles are there in the golden K-line diagram?
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
French scholars: the explicability of counter attack under optimal transmission theory
Jar批量管理小工具
Phpcms paid reading function Alipay payment
[IELTS reading] Wang Xiwei reading P3 (heading)