www.pudn.com > mobileMms.rar > SysConfig.java


package cn.netjava.mmsclient.util; 
 
import java.util.HashMap; 
import java.io.IOException; 
import java.io.FileInputStream; 
import java.util.ArrayList; 
import org.jdom.JDOMException; 
import java.util.List; 
import org.jdom.Document; 
import org.jdom.input.DOMBuilder; 
import org.jdom.Element; 
 
/** 
 *  
 * 

Title:中国移动mm7协议客户端 V0.1

*

Description:解析系统的XML配置

*

Company:蓝杰实训

* @author NetJava.cn * @version 0.1 */ public class SysConfig { private SysConfig() { load("./conf/sysConfig.xml"); } public HashMap hashmap = new HashMap(); static SysConfig conf = new SysConfig(); public static SysConfig getInstance() { return conf; } /**read XML file through the parameter and get value*/ public void load(String configFileName) { hashmap.clear(); try { hashmap = readXMLFile(configFileName); } catch (Exception e) { e.printStackTrace(); } } private HashMap readXMLFile(String inFile) throws Exception { Document doc; hashmap.clear(); try { DOMBuilder domb = new DOMBuilder(); doc = domb.build(new FileInputStream(inFile)); //SAXBuilder saxb = new SAXBuilder(); //doc = saxb.build(new FileInputStream(inFile)); Element root = doc.getRootElement(); //List children = root.getChildren(); int size = root.getChildren().size(); for (int i = 0; i < size; i++) { Element element = (Element) root.getChildren().get(i); hashmap.put(element.getName(), element.getTextTrim()); } } catch (JDOMException jdom) { System.err.println(jdom.getMessage()); System.exit(1); } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } return (this.hashmap); } public static void main(String[] args) { //Config conf = new Config(); // conf.load("./conf/DBConfig.xml"); System.out.println("hashmap = " + SysConfig.getInstance().hashmap); } }