global-config.xml: <?xml version="1.0" encoding="UTF-8"?> <sys-configure> <jdbc-info> <driver-class-name>com.mysql.jdbc.Driver</driver-class-name> <url>jdbc:mysql://localhost:3306/oa</url> <user-name>root</user-name> <password>root</password> </jdbc-info> <beans> <bean id="com.dao.DaoTest" class="com.dao.DaoTest" ></bean> <bean id="com.dao.DaoTest2" class="com.dao.DaoTest2"></bean> </beans> </sys-configure> GlobalConfig.java:从global-config.xml文件里获取数据 public class GlobalConfig { public static void main(String[] args) { GlobalConfig.getInstance(); DaoTest dao = (DaoTest) GlobalConfig.getInstance().getBean(DaoTest.class); } /** * singleton模式 */ private static GlobalConfig instance = new GlobalConfig(); private JdbcInfo jdbcInfo = new JdbcInfo(); private Map beanMap = new HashMap(); private Element rootElt; /** * 构造函数 */ private GlobalConfig(){ SAXBuilder sb = new SAXBuilder(); try { Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream("global-config.xml")); this.rootElt = doc.getRootElement(); this.initJdbcInfo(); this.initBeans(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 初始化JDBC信息 */ private void initJdbcInfo() { try { Element driverClassName = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/driver-class-name"); Element url = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/url"); Element username = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/user-name"); Element password = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/password"); this.getJdbcInfo().setDriverClassName(driverClassName.getText()); this.getJdbcInfo().setUrl(url.getText()); this.getJdbcInfo().setUsername(username.getText()); this.getJdbcInfo().setPassword(password.getText()); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 初始化Beans */ private void initBeans() { try { List beanList = XPath.selectNodes(rootElt, "//sys-configure/beans/bean"); for(Iterator iter = beanList.iterator();iter.hasNext();) { Element beanElt = (Element)iter.next(); String id = beanElt.getAttributeValue("id"); String className = beanElt.getAttributeValue("class"); try { Object obj = Class.forName(className).newInstance(); beanMap.put(id, obj); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取bean * @param c * @return */ public Object getBean(Class c) { return beanMap.get(c.getName()); } public static GlobalConfig getInstance() { return instance; } public JdbcInfo getJdbcInfo() { return jdbcInfo; } public void setJdbcInfo(JdbcInfo jdbcInfo) { this.jdbcInfo = jdbcInfo; } public Map getBeanMap() { return beanMap; } public void setBeanMap(Map beanMap) { this.beanMap = beanMap; } } XMLWriter.java:把数据写到XML文件 /** * * <selects> * <select> * <id>1</id> * <name>广东省</name> * </select> * <select> * <id>2</id> * <name>北京</name> * </select> * </selects> * * @author Administrator * */ public class XMLWriter { public static void main(String[] args) { Element rootElt = new Element("selects"); Element selectElt = new Element("select"); Element idElt = new Element("id"); idElt.addContent("1"); Element nameElt = new Element("name"); nameElt.addContent("广东省"); selectElt.addContent(idElt); selectElt.addContent(nameElt); rootElt.addContent(selectElt); //定义输出文件 Document doc = new Document(rootElt); XMLOutputter out = new XMLOutputter(); //out.setFormat(Format.getCompactFormat().setEncoding("gb2312")); try { //输出文件 out.output(doc, new FileOutputStream("c:/test.xml")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }