xml配置多类参数解析

此方法介绍了使用最普通的xml配置的参数,然后经过java文件解析,提供为工具类,使用的时候可以直接获取。类似于spring的bean,在bean中配置参数,然后由spring进行管理,此处不需要spring进行管理,方便简洁。直接使用标准的xml文件,在里面配置任意的名字(content,properties,urls,jdbc....),然后使用java工具类进行解析,具体如下。

目录存放结构:


example.xml

<?xml version="1.0" encoding="UTF-8"?>
<content>
	<!-- 适配器配置参数 -->
	<properties>
		<!-- 线程池 -->
		<property name="corePoolSize"><![CDATA[3]]></property><!-- 核心线程数-->
		<property name="maxPoolSize"><![CDATA[5]]></property><!-- 最大线程数-->
		<property name="keepAliveSeconds"><![CDATA[300]]></property>
		<property name="queueCapacity"><![CDATA[30]]></property>
		<property name="allowCoreThreadTimeOut"><![CDATA[false]]></property>
		<!-- 用户名和密码 -->
		<property name="userName"><![CDATA[admin]]></property>
		<property name="password"><![CDATA[admin123]]></property>
	</properties>
	<!-- 操作URL 用于判断操作类型   -->
	<urls>
		<url name=""><![CDATA[111]]></url>
	</urls>
	
	<!-- 被监控应用的数据库连接配置 (用于监控,不用作数据操作) -->
	<jdbc>
		<property name="databaseType"><![CDATA[oracle]]></property>
		<property name="driverClassName"><![CDATA[oracle.jdbc.driver.OracleDriver]]></property>
		<property name="url"><![CDATA[jdbc:oracle:thin:@172.17.15.1:1521:tax]]></property>
		<property name="username"><![CDATA[asopguoshui]]></property>
		<property name="password"><![CDATA[password]]></property>
		<property name="timeout"><![CDATA[5]]></property> <!-- jdbc连接超时设置 单位秒 -->
	</jdbc>
	<!-- WebLogic的配置参数 -->
	<weblogic>
		<property name="protocol"><![CDATA[t3]]></property>
		<property name="hostName"><![CDATA[127.0.0.1]]></property>
		<property name="portString"><![CDATA[7001]]></property>
		<property name="userName"><![CDATA[weblogic]]></property>
		<property name="password"><![CDATA[123*#abc]]></property>
	</weblogic>
</content>

解析xml的java类(注:文中的XmlUtils需要找同类文章的工具类 XmlParse)

package com.study.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public abstract class ParseProperties {

	private static HashMap<String, String> propertiesMap = new HashMap<String, String>();

	private static HashMap<String, String> urlsMap = new HashMap<String, String>();

	private static HashMap<String, String> jdbcMap = new HashMap<String, String>();

	private static HashMap<String, String> webLogicMap = new HashMap<String, String>();

	static {
		initProperties();
	}

	public static void initProperties() {
		try {
			Document doc = XmlUtils.parseForDoc(getInputStream("example.xml"));
			Element root = doc.getDocumentElement();
			List<Node> ruleItems = XmlUtils
					.parseForNodeList(root, "properties");
			for (Node rItem : ruleItems) {
				List<Node> subRules = XmlUtils.parseForNodeList(rItem,
						"property");
				for (Node subRule : subRules) {
					String propertyName = XmlUtils
							.getAttribute(subRule, "name");
					propertiesMap.put(propertyName, subRule.getTextContent());
				}
			}

			List<Node> urls = XmlUtils.parseForNodeList(root, "urls");
			for (Node rItem : urls) {
				List<Node> url = XmlUtils.parseForNodeList(rItem, "url");
				for (Node subRul : url) {
					String name = XmlUtils.getAttribute(subRul, "name");
					urlsMap.put(subRul.getTextContent(), name);
				}
			}

			List<Node> jdbcItems = XmlUtils.parseForNodeList(root, "jdbc");
			for (Node rItem : jdbcItems) {
				List<Node> subRules = XmlUtils.parseForNodeList(rItem,
						"property");
				for (Node subRule : subRules) {
					String propertyName = XmlUtils
							.getAttribute(subRule, "name");
					jdbcMap.put(propertyName, subRule.getTextContent());
				}
			}
			List<Node> webLogicItems = XmlUtils.parseForNodeList(root,
					"weblogic");
			for (Node rItem : webLogicItems) {
				List<Node> subRules = XmlUtils.parseForNodeList(rItem,
						"property");
				for (Node subRule : subRules) {
					String propertyName = XmlUtils
							.getAttribute(subRule, "name");
					webLogicMap.put(propertyName, subRule.getTextContent());
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getProperty(String key) {
		return propertiesMap.get(key);
	}

	public static String getJdbcProperty(String key) {
		return jdbcMap.get(key);
	}

	public static String getWebLogicProperty(String key) {
		return webLogicMap.get(key);
	}

	private static InputStream getInputStream(String filename) {
		InputStream stream = null;
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		if (classLoader != null) {
			stream = classLoader.getResourceAsStream(filename);
		} else {
			stream = ParseProperties.class.getClassLoader()
					.getResourceAsStream(filename);
		}
		return stream;
	}

	public static boolean hasLength(String str) {
		return hasLength((CharSequence) str);
	}

	public static boolean hasLength(CharSequence str) {
		return (str != null && str.length() > 0);
	}
}

测试类

@Test
	public void test1() {
		String s1 = ParseProperties.getWebLogicProperty("userName");
		System.out.println(s1);
	}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值