当前位置:网站首页>XML解析
XML解析
2022-07-24 05:18:00 【zsm030616】
一,在java中配置文件的三种配置位置及读取方式
(1)同包
InputStream in = Demo3.class.getResourceAsStream("config.xml");
(2)根路径
InputStream in = Demo1.class.getResourceAsStream("/db.properties");
(3)WIN-INF安全路径
InputStream in = Demo1.class.getResourceAsStream("/WBE-INF/db.properties");
提供了一组获得或关闭数据库对象的方法
<代码如下>
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 {// 静态方法执行一次 加载驱动一次
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);
}
}
/**
* 创建连接
*
* @return 连接
*/
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("数据库连接(关闭)成功");
}
}
①配置文件有啥好处?
.properties/ .xml/ .yml
(1)使代码更加灵活
(2)维护性更高
②各个地方的位置有啥讲究
同一层级:方便同步编码
根目录:一般不做频繁的更改
为什么要放在web-inf下?
因为web-inf属于安全目录(安全性更高),不可以直接访问,必须通过程序内部间接的访问
如果直接从页面访问,会给出异常

二,dom4j的使用
(1)selectNodes
(2)selectSingleNode
(3)attributValue
(4)getText
方法一
《代码如下》
package com.zsm.xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 读取各个位置的文件
* @author DXY
*2022年6月13日下午10:14:23
*/
public class Demo1 {
/**
* 方法一
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//同胞
InputStream in = Demo1.class.getResourceAsStream("db.properties");
//根目录
//InputStream in = Demo1.class.getResourceAsStream("/db.properties");
//.proerties有一个专门的类
Properties p = new Properties();
//此时p就加载db.properties中的所有信息
p.load(in);
System.out.println(p.getProperty("uname"));
System.out.println(p.getProperty("upwd"));
}
}
方法二
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 {
/**
* 方法二
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//同包
InputStream in = Demo2.class.getResourceAsStream("students.xml");
SAXReader sr = new SAXReader();
// //将配置文件中的所有内容获取到
Document doc = sr.read(in);
// System.out.println(doc.asXML());
// //selectNodes 根据标签返回集合
//获取子节点
// List<Element> studentEle = doc.selectNodes("students");
//打印长度
// System.out.println(studentEle.size());
//获取students下的子节点
// List<Element> studentEles = studentEle.get(0).selectNodes("student");
//打印该字节点的长度
// System.out.println(studentEles.size());
//selectNodes 根据标签名。返回单个标签 通常用于已经知晓该标签值出现一次
Element studentEle = (Element) doc.selectSingleNode("students");
//获取子节点
List<Element> stuEles = studentEle.selectNodes("student");
for (Element stuEle : stuEles) {
//attributeValue是拿到属性值
System.out.println(stuEle.attributeValue("sid"));
//getText拿到标签里的内容
System.out.println(stuEle.selectSingleNode("name").getText());
}
}
}
<效果图>

三,xpath的使用
(1)语法
/ 定位属性 @ 属性
(2)案例
浏览器Xpath使用
查找soo2的学生姓名
《代码演示》
方法三与方法四
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 {
// /代表层级,代表查找
// @精准定位
/**
* 方法三
*/
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());
/**
* 方法四
*/
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());
}
}
}
}
《效果图》
效果为方法四的代码

四,案例
《参考代码》
案例都基于以下代码块
<?xml version="1.0" encoding="UTF-8"?>
<!--
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>
(案例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;
/**
* 要求:获取所有action中的type值
* @author DXY
*2022年6月13日下午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"));
}
}
}
《效果图》

(案例二)
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;
/**
* 要求:获取第二个action的type值
* @author DXY
*2022年6月13日下午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"));
}
}
《效果图》

(案例三)
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;
/**
* 要求:获取第二个action的所有的forward的path
* @author DXY
*2022年6月13日下午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"));
}
}
}
《效果图》

(案例四)
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;
/**
* 要求:获取第二个action的第二个forward的path
* @author DXY
*2022年6月13日下午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"));
}
}
}
《效果图》
边栏推荐
猜你喜欢
随机推荐
canvas - 填充
New grammar 01_ ES6 new syntax
[common skills]
special effects - 鼠标移动,出现泡泡拖尾
special effects - 鼠标移动,出现星星拖尾
Some experience of using D2L package and related environment configuration
Hurry in!! Take you to understand what is multi file, and easily master the usage of extern and static C language keywords!!!
6. Draw a Bezier curve and a Bezier surface on the screen
Constructor_ Map constructor
special effects - 鼠标点击,出现烟花炸裂效果
Modify jupyter save path
函数_概括
随意写写 cookie,sessionStorage,localStorage和session
AttributeError: ‘NoneType‘ object has no attribute ‘shape‘
What is the function of key
新建 umi 项目,Error: Rendered more hooks 或者 Rendered fewer hooks
4. Draw a red triangle and a yellow square on the screen. Triangle in the back, small; Square in front, big. Using the fusion technology, the triangle can be seen through the square, and the source an
Learn AI linear regression from Li Mu. The code implementation from scratch is super detailed
Keywords_ 01return
输入10个人的名字,按从大到小排序输出









