XML解析工具類

  1.         由於以前在解析XML的時候,每次都要採用獲取XML中的屬性名來獲取對應的值,每一個bean類都要寫一個XML解析器。這將會使代碼變得更加冗餘,而沒辦法做到高度抽象。幸虧Java有自身的內省機制,Introspector與反射類似,主要是對Java Bean屬性、方法等的一種處理方法。
  2.  
  3. package com.ibm.cn.xml;
  4.  
  5. import java.beans.BeanInfo;
  6. importjava.beans.Introspector;
  7. import java.beans.PropertyDescriptor;
  8. import java.io.File;
  9. importjava.lang.reflect.Method;
  10. import java.util.ArrayList;
  11. import java.util.Iterator;
  12. import java.util.List;
  13.  
  14. import org.dom4j.Document;
  15. import org.dom4j.Element;
  16. importorg.dom4j.io.SAXReader;
  17.  
  18.  
  19. /**
  20.  * XML解析工具類
  21.  * @author javamickey
  22.  * @date 2012-11-01
  23.  * @version 2.0
  24.  */
  25. public class ParsersUtil {
  26.  
  27.  
  28.                 /**
  29.                  * 通過XML文件名,來解析XML對象,此函數只針對XML中存在一個bean值的情況
  30.                  * @paramfileName XML文件名
  31.                  * @param typebean類名
  32.                  * @return 單個bean對象
  33.                  * @throwsException
  34.                  */
  35.                 public static Object parserXMLByFileName(StringfileName, Class<?> type)
  36.                                                 throws Exception {
  37.                                 Class<? extends Object>beanClass = Class.forName(type.getName());
  38.                                 Object bean =beanClass.newInstance();
  39.                                 File inputXml = new File(fileName);
  40.                                 SAXReader saxReader = new SAXReader();
  41.                                 try {
  42.                                                 Document document =saxReader.read(inputXml);
  43.                                                 Element element =document.getRootElement();
  44.                                                 BeanInfo beanInfo =Introspector.getBeanInfo(type);
  45.                                                 PropertyDescriptor[]propertyDescriptors = beanInfo.getPropertyDescriptors();
  46.                                                 for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {
  47.                                                                 StringpropertyName = propertyDescriptor.getName();
  48.                                                                 for(Iterator<?> iterator = element.elementIterator(); iterator.hasNext();) {
  49.                                                                                 ElementemployeeElement = (Element) iterator.next();
  50.                                                                                 if(!propertyName.equals("class")) {
  51.                                                                                                 MethodreadMethod = propertyDescriptor.getWriteMethod();
  52.                                                                                                 Stringvalue = employeeElement.elementText(propertyName);
  53.                                                                                                 System.out.println("propertyName="+ propertyName + "value = " + value);
  54.                                                                                                 if(value != null) {
  55.                                                                                                                 readMethod.invoke(type,value);
  56.                                                                                                 }
  57.                                                                                 }
  58.                                                                 }
  59.                                                 }
  60.                                 } catch (Exception e) {
  61.                                                 e.printStackTrace();
  62.                                 }
  63.                                 return bean;
  64.                 }
  65.  
  66.                 /**
  67.                  * 通過XML文件名,來解析XML對象,此函數針對XML中存在一個或者多個bean值的情況
  68.                  * @paramfileName XML文件名
  69.                  * @param typebean類名
  70.                  * @return 返回一個List<bean>
  71.                  * @throwsException
  72.                  */
  73.                 public static List<Object>parserXMLByFileNameList(String fileName, Class<?> type)
  74.                                                 throws Exception {
  75.                                 Class<? extends Object>beanClass = Class.forName(type.getName());
  76.                                 List<Object> beanList = newArrayList<Object>();
  77.                                 File inputXml = new File(fileName);
  78.                                 SAXReader saxReader = newSAXReader();
  79.                                 try {
  80.                                                 Document document =saxReader.read(inputXml);
  81.                                                 Element element =document.getRootElement();
  82.                                                 BeanInfo beanInfo =Introspector.getBeanInfo(type);
  83.                                                 PropertyDescriptor[]propertyDescriptors = beanInfo.getPropertyDescriptors();
  84.                                                 for(Iterator<?> iterator = element.elementIterator(); iterator.hasNext();) {
  85.                                                                 Objectbean = beanClass.newInstance();
  86.                                                                 ElementemployeeElement = (Element) iterator.next();
  87.                                                                 for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {
  88.                                                                                 StringpropertyName = propertyDescriptor.getName();
  89.                                                                                 if(!propertyName.equals("class")) {
  90.                                                                                                 MethodreadMethod = propertyDescriptor.getWriteMethod();
  91.                                                                                                 Stringvalue = employeeElement.elementText(propertyName);
  92.                                                                                                 System.out.println("propertyName="+ propertyName + "value = " + value);
  93.                                                                                                 if(value != null) {
  94.                                                                                                                 readMethod.invoke(bean,value);
  95.  
  96.                                                                                                 }
  97.                                                                                 }
  98.                                                                 }
  99.                                                                 beanList.add(bean);
  100.                                                 }
  101.  
  102.                                 } catch (Exception e) {
  103.                                                 e.printStackTrace();
  104.  
  105.                                 }
  106.                                 return beanList;
  107.                 }
  108.  
  109. }

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