读取XML配置文件的工具类
来源:互联网 发布:怎么弄公司企业域名 编辑:程序博客网 时间:2024/06/05 20:14
1. 添加maven
2 XMLConfig.java
3. 使用
<dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version></dependency><dependency><!-- Jaxen is a universal Java XPath engine. --><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>1.1.6</version></dependency>
2 XMLConfig.java
package com.utils;import java.net.URL;import java.util.List;import org.dom4j.Document;import org.dom4j.Node;import org.dom4j.io.SAXReader;public class XMLConfig {private Document doc;public XMLConfig(String path){try {URL url = this.getClass().getClassLoader().getResource(path);SAXReader reader = new SAXReader();doc = reader.read(url);} catch (Exception e) {e.printStackTrace();}}public Document getDocument(){return doc;}public String getString(String path) throws Exception{Node node =doc.selectSingleNode(path);if(node!=null)return doc.selectSingleNode(path).getText();elsethrow new Exception(path + " not exists. ");}public int getInt(String path) throws Exception{Node node =doc.selectSingleNode(path);if(node!=null)return Integer.parseInt(doc.selectSingleNode(path).getText());elsethrow new Exception(path + " not exists. ");}@SuppressWarnings("unchecked")public List<Node> selectNodes(String path){return (List<Node>)doc.selectNodes(path);}}
3. 使用
try {XMLConfig config = new XMLConfig(path);String drive =config.getString("/configuration/environments/environment/dataSource/property[@name='driver']/@value") ;String url =config.getString("/configuration/environments/environment/dataSource/property[@name='url']/@value");String username =config.getString("/configuration/environments/environment/dataSource/property[@name='username']/@value");String password =config.getString("/configuration/environments/environment/dataSource/property[@name='password']/@value");int poolsize =config.getInt("/configuration/environments/environment/dataSource/property[@name='poolsize']/@value");pool = new JDBCPool(url, drive, username, password, poolsize);} catch (Exception e) {e.printStackTrace();}
阅读全文
0 0
- 读取XML配置文件的工具类
- XMLUtil工具类读取XML或properties格式的配置文件
- 读取配置文件的工具类
- XML 配置文件的读取
- 读取配置文件工具类
- 配置文件读取工具类
- 读取配置文件工具类
- 配置文件读取工具类
- 配置文件读取工具类
- 读取properties.xml配置文件的类
- 读取配置文件信息的工具类
- 读取properties配置文件的工具类
- properties配置文件的读取工具类
- java工具类之配置文件的读取
- 读取properties配置文件的工具类
- 读取properties配置文件的工具类
- XML配置文件的读取处理
- XML配置文件的读取(sax)
- 徐飞玉:自然语言理解及其应用学习笔记
- eclipse出现的问题
- 给Bean的属性赋值的好方法
- ARM的串口配置
- 一起看看MonoBehavior内部事件执行顺序
- 读取XML配置文件的工具类
- 几个比较实用的JVM进程分析命令
- C++ Primer 习题11.7
- [Amazon] Anagrams
- 【ionic3入门】ionic3初尝试之环境搭建
- Java基础总结(1)
- Spring 依赖注入
- PHP并发IO编程之路
- Head First设计模式笔记(模板方法模式)