利用dom4j读取XML配置文件
来源:互联网 发布:凤凰卫视直播软件apk 编辑:程序博客网 时间:2024/04/30 01:51
来自http://martin3000.iteye.com/blog/1326578
XMLReader类
- import java.io.File;
- import java.util.Iterator;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
- /**
- *
- * @author Martin3000
- *
- */
- public class XMLReader {
- // 配置文件名
- private static String filename = "conf.xml";
- private static Config config;
- /**
- * 从配置文件中读取参数并保存到Config类中,
- * 很多时候程序中会多次使用到配置中的参数,
- * 于是设置成静态方法,读取一次后就一直保存其中的参数,
- * 不再反复读取
- *
- * @return
- */
- public static Config loadconfig() {
- if (config == null)
- config = getconfig();
- return config;
- }
- private static Config getconfig() {
- Config config = new Config();
- try {
- File f = new File(filename);
- if (!f.exists()) {
- System.out.println(" Error : Config file doesn't exist!");
- System.exit(1);
- }
- SAXReader reader = new SAXReader();
- Document doc;
- doc = reader.read(f);
- Element root = doc.getRootElement();
- Element data;
- Iterator<?> itr = root.elementIterator("VALUE");
- data = (Element) itr.next();
- config.server = data.elementText("server").trim();
- config.user = data.elementText("user").trim();
- config.pass = data.elementText("pass").trim();
- config.port = data.elementText("port").trim();
- config.dbname = data.elementText("dbname").trim();
- } catch (Exception ex) {
- System.out.println("Error : " + ex.toString());
- }
- return config;
- }
- }
XML文格式(conf.xml)
- <?xml version="1.0" encoding="UTF-8"?>
- <CONFIG>
- <VALUE>
- <!-- mysql连接设置 -->
- <server>127.0.0.1</server>
- <dbname>users</dbname>
- <user>root</user>
- <pass>pass</pass>
- <port>3306</port>
- </VALUE>
- </CONFIG>
0 0
- 利用dom4j读取XML配置文件
- java读取xml数据库配置文件--利用dom4j.jar
- XML配置文件的读取--DOM4J
- 使用dom4j读取xml配置文件
- 利用dom4j读取xml文件
- 利用dom4j读取xml文件
- 利用dom4j读取XML文件
- 利用dom4j读取XML文件
- 利用dom4j--读取xml文件
- dom4j 读取xml 数据库配置文件 dataconfig
- DOM4J 读取XML配置文件进行数据库连接
- drp—Dom4j读取xml配置文件
- 利用Dom4j+xpath读取XML文档
- 利用dom4j读取完整的xml文档
- Java读取XML配置文件详细总结(dom4j方式)
- Dom4j解析XML来读取数据库配置文件信息
- dom4j读取spring配置文件
- dom4j读取配置文件
- Android自定义类似ProgressDialog效果的Dialog
- CentOS 5.5下安装rsync笔记
- R-自画蜡烛图R CandleChart
- java.sql.SQLException: Before start of result set
- wpf 关于textbox的spellcheck无效
- 利用dom4j读取XML配置文件
- java格式化数字DecimalFormat
- 解决Hibernate查找视图有一个空值的时候返回的对象为null的问题
- Plus One
- 从C++到Objective-C的学习笔记
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- ACCESS数据库的注入实现之常见的select查询1
- Elance总结
- 大数据量表的优化查询 - 发条