当前位置:网站首页>XML parsing
XML parsing
2022-07-24 06:27:00 【zsm030616】
One , stay java Three configuration locations and reading methods of configuration files in
(1) Same bag
InputStream in = Demo3.class.getResourceAsStream("config.xml");
(2) The root path
InputStream in = Demo1.class.getResourceAsStream("/db.properties");
(3)WIN-INF Safe path
InputStream in = Demo1.class.getResourceAsStream("/WBE-INF/db.properties");
Provides a set of methods to get or close database objects
< The code is as follows >
package com.zsm.xml;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBHelper {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// The static method executes once Load the driver once
try {
InputStream is = DBHelper.class
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* Create connection
*
* @return Connect
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Statement stmt) {
if (null != stmt) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
close(rs);
close(stmt);
close(conn);
}
public static boolean isOracle() {
return "oracle.jdbc.driver.OracleDriver".equals(driver);
}
public static boolean isSQLServer() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
}
public static boolean isMysql() {
return "com.mysql.jdbc.Driver".equals(driver);
}
public static void main(String[] args) {
Connection conn = DBHelper.getConnection();
DBHelper.close(conn);
System.out.println("isOracle;" + isOracle());
System.out.println("isSQLServer;" + isSQLServer());
System.out.println("isMysql;" + isMysql());
System.out.println(" Database connection ( close ) success ");
}
}
① What are the benefits of configuration files ?
.properties/ .xml/ .yml
(1) Make the code more flexible
(2) Higher maintainability
② What about the location of each place
At the same level : Convenient for synchronous coding
root directory : Usually do not make frequent changes
Why put it in web-inf Next ?
because web-inf It belongs to the security directory ( safer ), No direct access to , It must be accessed indirectly through the program
If you visit directly from the page , Will give an exception

Two ,dom4j Use
(1)selectNodes
(2)selectSingleNode
(3)attributValue
(4)getText
Method 1
《 The code is as follows 》
package com.zsm.xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Read files in various locations
* @author DXY
*2022 year 6 month 13 On the afternoon of Sunday 10:14:23
*/
public class Demo1 {
/**
* Method 1
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Compatriot
InputStream in = Demo1.class.getResourceAsStream("db.properties");
// root directory
//InputStream in = Demo1.class.getResourceAsStream("/db.properties");
//.proerties There is a special class
Properties p = new Properties();
// here p Just load db.properties All information in
p.load(in);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upwd"));
}
}
Method 2
package com.zsm.xml;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo2 {
/**
* Method 2
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Same bag
InputStream in = Demo2.class.getResourceAsStream("students.xml");
SAXReader sr = new SAXReader();
// // Get all the contents in the configuration file to
Document doc = sr.read(in);
// System.out.println(doc.asXML());
// //selectNodes Return the set according to the tag
// Get child nodes
// List<Element> studentEle = doc.selectNodes("students");
// Print length
// System.out.println(studentEle.size());
// obtain students Child nodes under
// List<Element> studentEles = studentEle.get(0).selectNodes("student");
// Print the length of the word node
// System.out.println(studentEles.size());
//selectNodes By tag name . Returns a single label It is usually used to know that the tag value appears once
Element studentEle = (Element) doc.selectSingleNode("students");
// Get child nodes
List<Element> stuEles = studentEle.selectNodes("student");
for (Element stuEle : stuEles) {
//attributeValue Get the attribute value
System.out.println(stuEle.attributeValue("sid"));
//getText Get the contents of the label
System.out.println(stuEle.selectSingleNode("name").getText());
}
}
}
< design sketch >

3、 ... and ,xpath Use
(1) grammar
/ Location attribute @ attribute
(2) Case study
browser Xpath Use
lookup soo2 Student name of
《 Code demonstration 》
Method 3 and method 4
package com.zsm.xml;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Demo3 {
public static void main(String[] args) throws Exception {
// / Representative hierarchy , For finding
// @ Precise positioning
/**
* Method 3
*/
InputStream in = Demo3.class.getResourceAsStream("students.xml");
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
// Element stus002 = (Element) doc.selectSingleNode("/students/student[@sid='s002']/name");
// System.out.println(stus002.getText());
/**
* Method four
*/
Element studentEle = (Element) doc.selectSingleNode("students");
List<Element> stuEles = studentEle.selectNodes("student");
for (Element stuEle : stuEles) {
if("s002".equals(stuEle.attributeValue("sid"))) {
System.out.println(stuEle.selectSingleNode("name").getText());
}
}
}
}
《 design sketch 》
The effect is the code of method 4

Four , Case study
《 Reference code 》
The cases are based on the following code blocks
<?xml version="1.0" encoding="UTF-8"?>
<!--
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>
( Case study 1)
package com.zsm.xml;
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;
/**
* requirement : Get all action Medium type value
* @author DXY
*2022 year 6 month 13 On the afternoon of Sunday 11:42:12
*/
public class action01 {
public static void main(String[] args) throws Exception {
InputStream in = Demo3.class.getResourceAsStream("config.xml");
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
Element type = (Element) doc.selectSingleNode("config");
List<Element> action = type.selectNodes("action");
for (Element actions : action) {
System.out.println(actions.attributeValue("type"));
}
}
}
《 design sketch 》

( Case 2 )
package com.zsm.xml;
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;
/**
* requirement : Get the second action Of type value
* @author DXY
*2022 year 6 month 13 On the afternoon of Sunday 11:42:12
*/
public class action2 {
public static void main(String[] args) throws Exception {
InputStream in = Demo3.class.getResourceAsStream("config.xml");
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
Element type2 = (Element) doc.selectSingleNode("/config/action[@path='/loginAction']");
System.out.println(type2.attributeValue("type"));
}
}
《 design sketch 》

( Case three )
package com.zsm.xml;
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;
/**
* requirement : Get the second action All of forward Of path
* @author DXY
*2022 year 6 month 13 On the afternoon of Sunday 11:42:12
*/
public class action3 {
public static void main(String[] args) throws Exception {
InputStream in = Demo3.class.getResourceAsStream("config.xml");
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
//Element type2 = (Element) doc.selectSingleNode("/config/action[@path='/loginAction']/forward");
//System.out.println(type2.attributeValue("path"));
Element forward = (Element) doc.selectSingleNode("config/action[@path='/loginAction']");
List<Element> path = forward.selectNodes("forward");
for (Element paths : path) {
System.out.println(paths.attributeValue("path"));
}
}
}
《 design sketch 》

( Case four )
package com.zsm.xml;
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;
/**
* requirement : Get the second action The second forward Of path
* @author DXY
*2022 year 6 month 13 On the afternoon of Sunday 11:42:12
*/
public class action4 {
public static void main(String[] args) throws Exception {
InputStream in = Demo3.class.getResourceAsStream("config.xml");
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
//Element type2 = (Element) doc.selectSingleNode("/config/action[@path='/loginAction']/forward");
//System.out.println(type2.attributeValue("path"));
Element forward = (Element) doc.selectSingleNode("config/action[@path='/loginAction']");
List<Element> path = forward.selectNodes("forward[@name='success']");
for (Element paths : path) {
System.out.println(paths.attributeValue("path"));
}
}
}
《 design sketch 》
边栏推荐
- IP notes (11)
- [219] what is the difference between app testing and web testing?
- Sorting of common AR and MR head mounted display devices
- Flink function (1): rich function
- Data set and pre training model
- 【测试工具】
- Map the intranet to the public network [no public IP required]
- Flink function (2): checkpointedfunction
- Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]
- IP notes (6)
猜你喜欢
![[214] what is an automation framework](/img/bb/24c35613c49357258876ce3f1611ee.jpg)
[214] what is an automation framework

Unity (III) three dimensional mathematics and coordinate system

IP lesson summary (3)

IP课笔记(5)

利用内网穿透,实现公网访问内网

leetcode剑指offer JZ23:链表中环的入口节点

Do not rent servers, build your own personal business website (3)

如何建立一个仪式感点满的网站,并发布到公网 2-2

Simple three-step fast intranet penetration

How to build a website full of ritual sense and publish it on the public website 2-2
随机推荐
IP笔记(12)
Homework in the second week
List of problems in the re disk guidance of the project
Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]
The public network uses Microsoft Remote Desktop remote desktop to work remotely at any time
Ia note 1
[no need for public IP] configure a fixed public TCP port address for remote desktop raspberry pie
IP作业(6)
ip作业(1)
测试经理/测试组长/测试主管面试题
Basic knowledge of unity and the use of some basic APIs
【301】怪诞行为学-可预测的非理性
[218] what are the advantages and disadvantages of CS architecture and BS architecture and data on the server and client?
Do not rent servers, build your own personal business website (2)
Top 10 vulnerability assessment and penetration testing tools
Leetcode sword finger offer jz73 flip word sequence
IP笔记(7)
【无需公网IP】为远程桌面树莓派配置固定的公网TCP端口地址
Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
leetcode 不用加减乘除算加法 || 二进制中1的个数