xml文件
<?xml version="1.0" encoding="UTF-8"?>
<sys-configure>
<jdbc-info>
<dirver-class-name>com.mysql.jdbc.Driver</dirver-class-name>
<url>jdbc:mysql://127.0.0.1:3306/mysql1</url>
<user-name>root</user-name>
<password>pengbo</password>
</jdbc-info>
<beans>
<bean id="com.gzbb.item.dao.ItemDao" class="com.gzbb.item.dao.ItemDaoImpl"/>
</beans>
</sys-configure>
-----------------------------------------------------
java文件
package com.gzbb.item.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import com.gzbb.item.dao.ItemDao;
public class GobalConfigure {
private static GobalConfigure instance = new GobalConfigure();
private final static String CONFIG_FILE_NAME="gobal_configure.xml";
private Element rootElement;
private JdbcInfo jdbcInfo = new JdbcInfo();
private Map beanMap = new HashMap();
private GobalConfigure() {
SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE_NAME));
rootElement = document.getRootElement();
initJdbcInfo();
initBeans();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
getInsatance();
// getInsatance().getBean("com.gzbb.item.dao.ItemDao");
getInsatance().getBean(ItemDao.class);
}
public JdbcInfo getJdbcInfo() {
return jdbcInfo;
}
public static GobalConfigure getInsatance() {
return instance;
}
public void initBeans()
{
try {
List elList = (List)XPath.selectNodes(rootElement, "//sys-configure/beans/bean");
for(Iterator it = elList.iterator();it.hasNext();)
{
Element el = (Element)it.next();
String id = el.getAttributeValue("id");
String name = el.getAttributeValue("class");
//实例化Dao
//实例化的类需要有公共构造函数
//Object obj = Class.forName(name).getMethods();
Object obj = Class.forName(name).newInstance();
beanMap.put(id, obj);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private Object getBean(Class c)
{
return beanMap.get(c.getName());
}
private void initJdbcInfo()
{
try {
Element driverClassNameElt = (Element)XPath.selectSingleNode(rootElement, "//sys-configure/jdbc-info/dirver-class-name");
Element urlElt = (Element)XPath.selectSingleNode(rootElement, "//sys-configure/jdbc-info/url");
Element userNameElt = (Element)XPath.selectSingleNode(rootElement, "//sys-configure/jdbc-info/user-name");
Element passwordElt = (Element)XPath.selectSingleNode(rootElement, "//sys-configure/jdbc-info/password");
getJdbcInfo().setDriverName(driverClassNameElt.getText());
getJdbcInfo().setUrl(urlElt.getText());
getJdbcInfo().setUserName(userNameElt.getText());
getJdbcInfo().setPassword(passwordElt.getText());
System.out.println(getJdbcInfo().toString());
} catch (JDOMException e) {
e.printStackTrace();
}
}
}
-----------------------------------------------------------------
写XML文件
package com.gzbb.item.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class XMLWriter {
public static void main(String[] agrs)
{
Element rootElt = new Element("selects");
Element elt1 = new Element("select");
Element elt2 = new Element("id");
elt2.addContent("1");
Element elt2_1 = new Element("name");
elt2_1.addContent("黑龙江");
elt1.addContent(elt2);
elt1.addContent(elt2_1);
rootElt.addContent(elt1);
Document dc = new Document(rootElt);
XMLOutputter xmlOutputter = new XMLOutputter();
xmlOutputter.setFormat(Format.getCompactFormat().setEncoding("gbk"));
System.out.println(xmlOutputter.outputString(dc));
try {
xmlOutputter.output(dc, new FileOutputStream("c:/text.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}