位置:海鸟网 > IT > XML >

JDOM对XML文件的读取

  先要引入JDOM-1.1开发包中目录中lib下的jar包和build下jdom.jar

xml文件:(gobal-configure.xml)

<?xml version="1.0" encoding="UTF-8"?>
<sys-configure>
<jdbc-info>
<driver-class-name>oracle.jdbc.driver.OracleDriver</driver-class-name>
<url>jdbc:oracle:thin:@127.0.0.1:1515:sxt</url>
<use-name>frank</use-name>
<password>frank</password>
</jdbc-info>
<beans>
<bean id="com.bjsxt.drp.business.usermgr.dao.UserDao" class="com.bjsxt.drp.business.usermgr.dao.UserDao4MySqlImpl"/>
<bean id="com.bjsxt.drp.business.usermgr.dao.UserDao" class="com.bjsxt.drp.business.usermgr.dao.UserDao4MySqlImpl"/>

</beans>
</sys-configure>

JdbcInfo.java(把读取的结果封装在里面)

package com.bjsxt.xml.training;

public class JdbcInfo {
public String toString() {
// TODO Auto-generated method stub
return "driverClassName="+driverClassName+",url="+url+",userName="+userName+",password="+password;
}

private String driverClassName;

private String url;
private String userName;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}

}

GlobalConfigure.java(具体的读取文件)
package com.bjsxt.xml.training;

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.bjsxt.drp.business.usermgr.dao.UserDao;


public class GlobalConfigure {
private static GlobalConfigure instance = new GlobalConfigure();

private static final String CONFIG_FILE_NAME = "gobal-configure.xml";

private Element rootElt;

private JdbcInfo jdbcInfo = new JdbcInfo();

private Map beanMap = new HashMap();

private GlobalConfigure(){
SAXBuilder sb =new SAXBuilder();
try {
Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResource(CONFIG_FILE_NAME));
this.rootElt = doc.getRootElement();
initJdbcInfo();
initBeans();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static GlobalConfigure getInstance(){

return instance;
}
private void initJdbcInfo(){
try {
Element driverClassNameElt = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/driver-class-name");

jdbcInfo.setDriverClassName(driverClassNameElt.getText());

Element urlElt = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/url");
jdbcInfo.setUrl(urlElt.getText());


Element useNameElt = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/use-name");
jdbcInfo.setUserName(useNameElt.getText());


Element passwordElt = (Element) XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/password");
jdbcInfo.setPassword(passwordElt.getText());


} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 

}
public static void main(String[] args){
System.out.println(GlobalConfigure.getInstance().getJdbcInfo());
UserDao userDao = (UserDao)GlobalConfigure.getInstance().getBean(UserDao.class);

}
public JdbcInfo getJdbcInfo() {
return jdbcInfo;
}

public Object getBean(Class c){
return beanMap.get(c.getName());

}

private void initBeans(){
List beanList;
try {
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");
System.out.println("id="+id);
System.out.println("class="+ className);
Object obj = Class.forName(className).newInstance();
beanMap.put(id,obj);
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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();
}
}

}