使用JDom來處理XML配置文件

使用JDom來處理XML配置文件

本文主要介紹使用JDom來解析XML文件

以下示例一共有四個類:類 Student,類 Teacher,類 JdbcInfo,類 ConfigurationUtils

和一個配置文件:config.xml.

Student:封裝了sid,sname 的get和set 方法。

Teacher:封裝了tid,tname 的get和set 方法。

JdbcInfo:封裝了driverName,url,username,password 的get和set 方法。

ConfigurationUtils:主要是使用JDom來讀取config.xml的數據庫配置文件信息,並實例化Student和Teacher。

文件 config.xml:裏面配置了數據庫的訪問信息,還有Student和Teacher的持久化bean配置信息。

 

具體如下:

文件 config.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <Oracle-Info>
  <driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
  <url>jdbc:oracle:thin:@192.168.1.100:1521:test</url>
  <username>test</username>
  <password>test</password>
 </Oracle-Info>
 
 <beans-Info>
  <bean id="student" class="beanInfo.Student"/>
  <bean id="teacher" class="beanInfo.Teacher"/>
 </beans-Info>
</configuration>

 

類 Student

package beanInfo;

 

public class Student implements java.io.Serializable {

 // Fields

 private String sid;
 private String sname;

 // Constructors

 /** default constructor */
 public Student() {
  System.out.println("類  Student 實例化");
 }

 /** minimal constructor */
 public Student(String sid) {
  this.sid = sid;
 }

 /** full constructor */
 public Student(String sid, String sname) {
  this.sid = sid;
  this.sname = sname;
 }

 // Property accessors

 public String getSid() {
  return this.sid;
 }

 public void setSid(String sid) {
  this.sid = sid;
 }

 public String getSname() {
  return this.sname;
 }

 public void setSname(String sname) {
  this.sname = sname;
 }
}

 

類 Teacher

package beanInfo;


public class Teacher implements java.io.Serializable {

 // Fields

 private String tid;
 private String tname;

 // Constructors

 /** default constructor */
 public Teacher() {
  System.out.println("類  Teacher 實例化");
 }

 /** minimal constructor */
 public Teacher(String tid) {
  this.tid = tid;
 }

 /** full constructor */
 public Teacher(String tid, String tname) {
  this.tid = tid;
  this.tname = tname;
 }

 // Property accessors

 public String getTid() {
  return this.tid;
 }

 public void setTid(String tid) {
  this.tid = tid;
 }

 public String getTname() {
  return this.tname;
 }

 public void setTname(String tname) {
  this.tname = tname;
 }
}

 

類 JdbcInfo

package mapping;

 

public class JdbcInfo {

 private String driverName;
 
 private String url;
 
 private String username;
 
 private String password;
 
 public String getDriverName() {
  return driverName;
 }

 public void setDriverName(String driverName) {
  this.driverName = driverName;
 }

 public String getUrl() {
  return url;
 }

 public void setUrl(String url) {
  this.url = url;
 }

 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;
 }
}

類 ConfigurationUtils

package mapping;


public class ConfigurationUtils {


 private static ConfigurationUtils configuration = new ConfigurationUtils();
 
 private static final String CONFIG_FILE_NAME ="config.xml";
 
 private Element rootElt;
 
 private JdbcInfo jdbcInfo = new JdbcInfo();
 
 private Map beanMap = new HashMap();
 
 private ConfigurationUtils(){
  SAXBuilder sb = new SAXBuilder();
  try {
   Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(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();
  }
 }
 
 private void initjdbcInfo(){
  try {
   Element driverName = (Element) XPath.selectSingleNode(rootElt, "//configuration/Oracle-Info/driver-name");
   jdbcInfo.setDriverName(driverName.getText());
   Element url = (Element) XPath.selectSingleNode(rootElt, "//configuration/Oracle-Info/url");
   jdbcInfo.setDriverName(url.getText());   
   Element username = (Element) XPath.selectSingleNode(rootElt, "//configuration/Oracle-Info/username");
   jdbcInfo.setDriverName(username.getText());   
   Element password = (Element) XPath.selectSingleNode(rootElt, "//configuration/Oracle-Info/password");
   jdbcInfo.setDriverName(password.getText());   
  } catch (JDOMException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 private void initBeans(){
  try {
   List list = XPath.selectNodes(rootElt, "//configuration/beans-Info/bean");
   for(Iterator it = list.iterator();it.hasNext();)
   {
    Element beanElt = (Element)it.next();
    String id = beanElt.getAttributeValue("id");
    String className = beanElt.getAttributeValue("class");
    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();
  }
 }
 
 public static ConfigurationUtils getConfiguration(){
  return configuration;
 }
 
 public JdbcInfo getjdbcInfo() {
  return jdbcInfo;
 } 
 
 public Object getBean(Class c){
  return beanMap.get(c.getName());
 }
 

調用如下:

 public static void main(String[] args) {
  
  String driverName = ConfigurationUtils.getConfiguration().getjdbcInfo().getDriverName();
  String url = ConfigurationUtils.getConfiguration().getjdbcInfo().getUrl();
  String username = ConfigurationUtils.getConfiguration().getjdbcInfo().getUsername();
  String password = ConfigurationUtils.getConfiguration().getjdbcInfo().getPassword();
  
  Student student = (Student)ConfigurationUtils.getConfiguration().getBean(Student.class);
  Teacher teacher = (Teacher)ConfigurationUtils.getConfiguration().getBean(Student.class);
 }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章