XML解析

这篇博客介绍了Java中配置文件的三种加载位置及其读取方式,包括同包、根路径和WEB-INF安全路径。同时,文章详细讲解了DOM4J库解析XML配置文件的方法,如selectNodes、selectSingleNode和attributeValue等,以Hibernate配置文件为例,展示了如何读取数据库连接、调试设置和实体映射等关键信息。
摘要由CSDN通过智能技术生成

Java中配置文件的三种配置位置以及读取方式:

同包:Demo1.class.getResourceAstream("db.properties");

根路径:Demo1.class.getResourceAstream("/db.properties");

WIN-INF安全路径:context.getResourceAstream("/WEB-INF/db.properties");

节点读取的方式:

selectNodes:读取节点

selectSingleNode:读取头节点

attributeValue:获取属性值

getText:获取节点文本

Xpath的使用:

/:定位路径 @:属性

具体使用方法请参考下图:

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
	<session-factory>
		<!-- 1.数据库相关 -->
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/t207?useUnicode=true&amp;characterEncoding=UTF-8</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 2.调试相关 -->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>

		<!-- 开启二级缓存 -->
		<property name="hibernate.cache.use_second_level_cache">true</property>
		<!-- 开启查询缓存 -->
		<property name="hibernate.cache.use_query_cache">true</property>
		<!-- EhCache驱动 -->
		<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
		<!-- 3.实体类映射文件 -->
		<!-- <mapping resource="entity/user.hbm.xml"/> -->
		<mapping resource="entity/worker.hbm.xml" />
		<mapping resource="entity/student.hbm.xml" />
		<mapping resource="entity/user.hbm.xml" />
		<mapping resource="entity/order.hbm.xml" />
		<mapping resource="entity/orderItem.hbm.xml" />
		<mapping resource="entity/treeNode.hbm.xml" />
		<mapping resource="entity/book.hbm.xml" />
		<mapping resource="entity/category.hbm.xml" />
	</session-factory>





</hibernate-configuration>	
package test;

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 Zy2 {

	public static void main(String[] args) throws Exception {
		InputStream is = Zy2.class.getResourceAsStream("hibernate.cfg.xml");
		//创建xml文件读取工具类对象SaxReader
		SAXReader sr =new SAXReader();
		//读取.xml配置文件
		Document doc = sr.read(is);
		List<Node> nodes = doc.selectNodes("//*");
		System.out.println(nodes.size());
        for (Node node : nodes) {
        	 Element el =(Element)node;
        	 System.out.println(node.getText());
        	 if(el.attributeValue("name")!=null) {
        	 System.out.println(el.attributeValue("name"));
        	 }
        	 if(el.attributeValue("resource")!=null) {
        		 System.out.println(el.attributeValue("resource"));
        	 }
		}
	}

}


 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值