⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlconfig.java

📁 Email营销是在用户事先许可的前提下
💻 JAVA
字号:
package com.qq.util;

/**
 * Title:        锟斤拷锟斤拷锟斤拷锟斤拷锟街э拷锟?-config.xml锟斤拷锟斤拷锟饺≈э拷锟?
 * Description: 使锟斤拷sun JAXP XML API
 * Copyright:    Copyright (c) 1999
 * Company:
 * @author
 * @version 1.0
 * config.xml Example:
 <all-config>
	<config id="DBConnDefault">
		<parameter value="informix" name="userID"/>
		<parameter value="informix" name="password"/>
		<parameter value="jdbc/mobile" name="dataSourceName"/>
	</config>
	<config id="DBConnBill">
		<parameter value="informix" name="userID"/>
		<parameter value="informix" name="password"/>
		<parameter value="jdbc/bill" name="dataSourceName"/>
	</config>
</all-config>
 */
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.InputStream;
import java.io.IOException;

public class XMLConfig {
	private static XMLConfig xmlConfig = null;

	/** @todo 锟斤拷取config.xml锟斤拷Document */
	private static Document configDocument = null;

	private Document getConfigDocument() throws SAXParseException, IOException,
			ParserConfigurationException, SAXException {
		if (configDocument != null)
			return configDocument;
		return getConfigDocument("/dbconfig.xml");
	}

	private Document getConfigDocument(String fileName)
			throws SAXParseException, IOException,
			ParserConfigurationException, SAXException {
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		InputStream in = this.getClass().getResourceAsStream(fileName);
		configDocument = docBuilder.parse(in);
		in.close();
		return configDocument;
	}

	/** @todo 取指锟斤拷group id 锟斤拷锟斤拷锟轿? paraName 锟侥诧拷锟斤拷值 */
	public static String getParameter(String groupId, String paraName)
			throws SAXParseException, IOException,
			ParserConfigurationException, SAXException {
		Element node = getElementFromConfigId(groupId);		
		NodeList paraList = node.getElementsByTagName("parameter");
		for (int j = 0; j < paraList.getLength(); j++) {
			Element para = (Element) paraList.item(j);
			String name = para.getAttribute("name");
			String value = para.getAttribute("value");
			if (name.endsWith(paraName))
				return value;
		}
		return null;
	}

	/** @todo 锟叫讹拷指锟斤拷Config ID 锟斤拷 config.xml锟斤拷锟角凤拷锟斤拷冢锟? */
	public static boolean isExistConfigId(String configId)
			throws SAXParseException, IOException,
			ParserConfigurationException, SAXException {
		return (getElementFromConfigId(configId) == null) ? false : true;
	}

	/** @todo 锟斤拷取指锟斤拷 Config ID 锟斤拷 Element*/
	private static Element getElementFromConfigId(String groupId)
			throws SAXParseException, IOException,
			ParserConfigurationException, SAXException {
		if (xmlConfig == null)
			xmlConfig = new XMLConfig();
		if (groupId == null)
			return null;
		Document doc = xmlConfig.getConfigDocument();
		Element rootElement = doc.getDocumentElement();
		//rootElement.normalize ();
		NodeList servletList = rootElement.getElementsByTagName("config");
		for (int i = 0; i < servletList.getLength(); i++) {
			Element node = (Element) servletList.item(i);
			String id = node.getAttribute("id");
			if (id.equals(groupId))
				return node;
		}
		return null;
	}

	private XMLConfig() {
	}

	/** @todo Test dic.util.XMLConfig */
	public static void main(String args[]) {
		try {
			String user = XMLConfig.getParameter("DBConnDefault", "userID");
			System.out.println("oracleDB**********UserID=" + user);
		} catch (Exception err) {
			err.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -