Java读取XML配置文件详细总结(dom4j方式)

最初的想法是不把mysql的连接参数写到程序中,因为每次要修改参数总是很麻烦,于是想找到一种能够方便修改的方式,后来就找到了通过XML文件存储程序参数的方法。本文中使用dom4j读取xml文件



XMLReader类
Java代码 复制代码 收藏代码
  1. import java.io.File;
  2. import java.util.Iterator;
  3. import org.dom4j.Document;
  4. import org.dom4j.Element;
  5. import org.dom4j.io.SAXReader;
  6. /**
  7. *
  8. * @author Martin3000
  9. *
  10. */
  11. public class XMLReader {
  12. // 配置文件名
  13. private static String filename = "conf.xml";
  14. private static Config config;
  15. /**
  16. * 从配置文件中读取参数并保存到Config类中,
  17. * 很多时候程序中会多次使用到配置中的参数,
  18. * 于是设置成静态方法,读取一次后就一直保存其中的参数,
  19. * 不再反复读取
  20. *
  21. * @return
  22. */
  23. public static Config loadconfig() {
  24. if (config == null)
  25. config = getconfig();
  26. return config;
  27. }
  28. private static Config getconfig() {
  29. Config config = new Config();
  30. try {
  31. File f = new File(filename);
  32. if (!f.exists()) {
  33. System.out.println(" Error : Config file doesn't exist!");
  34. System.exit(1);
  35. }
  36. SAXReader reader = new SAXReader();
  37. Document doc;
  38. doc = reader.read(f);
  39. Element root = doc.getRootElement();
  40. Element data;
  41. Iterator<?> itr = root.elementIterator("VALUE");
  42. data = (Element) itr.next();
  43. config.server = data.elementText("server").trim();
  44. config.user = data.elementText("user").trim();
  45. config.pass = data.elementText("pass").trim();
  46. config.port = data.elementText("port").trim();
  47. config.dbname = data.elementText("dbname").trim();
  48. } catch (Exception ex) {
  49. System.out.println("Error : " + ex.toString());
  50. }
  51. return config;
  52. }
  53. }
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;

	}
}

Config类

读取文件中的配置到Config类中,这里主要是Mysql的连接配置
Java代码 复制代码 收藏代码
  1. /**
  2. *
  3. * @author Marin3000
  4. *
  5. */
  6. public class Config {
  7. public String server;
  8. public String user;
  9. public String pass;
  10. public String port;
  11. public String dbname;
  12. public String getConnString() {
  13. String connString = "jdbc:mysql://" + server + ":" + port + "/"
  14. + dbname + "?user=" + user + "&password=" + pass
  15. + "&useUnicode=true&characterEncoding=UTF-8";
  16. return connString;
  17. }
  18. }
/**
 * 
 * @author Marin3000
 *
 */
public class Config {

	public String server;
	public String user;
	public String pass;
	public String port;
	public String dbname;

	public String getConnString() {

		String connString = "jdbc:mysql://" + server + ":" + port + "/"
				+ dbname + "?user=" + user + "&password=" + pass
				+ "&useUnicode=true&characterEncoding=UTF-8";
		return connString;

	}

}

XML文格式(conf.xml)
Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <CONFIG>
  3. <VALUE>
  4. <!-- mysql连接设置 -->
  5. <server>127.0.0.1</server>
  6. <dbname>users</dbname>
  7. <user>root</user>
  8. <pass>pass</pass>
  9. <port>3306</port>
  10. </VALUE>
  11. </CONFIG>
<?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> 

好了 再也不会有在源程序里修改配置的烦恼了,附件里有dom4j的jar包下载 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值