天天看點

根據XML配置規則導入Excel資料(一)定義XML規則

定義xml 導入規則。基本上為每個值對象,都應該對應一個配置bean節點。

首先:為xml 檔案制定xsd驗證檔案。

<?xml version="1.0" encoding="utf-8"?> 

<xsd:schema xmlns="http://3g.ahong.com/schema/mrp/beans" 

  xmlns:xsd="http://www.w3.org/2001/xmlschema" targetnamespace="http://3g.ahong.com/schema/mrp/beans"> 

  <xsd:element name="beans"> 

    <xsd:complextype> 

      <xsd:sequence> 

        <!-- required ref , or will bring some error --> 

        <xsd:element ref="bean" minoccurs="1" maxoccurs="unbounded"></xsd:element> 

      </xsd:sequence> 

    </xsd:complextype> 

  </xsd:element> 

  <xsd:element name="bean"> 

        <xsd:element ref="property" minoccurs="1" maxoccurs="unbounded"/> 

      <xsd:attribute name="classname" type="xsd:string" use="required" /> 

      <xsd:attribute name="filename" type="xsd:string" use="required" /> 

      <xsd:attribute name="head" type="xsd:int" use="optional" /> 

      <xsd:attribute name="from" type="xsd:int" use="optional" /> 

  <xsd:element name="property"> 

      <xsd:attribute name="name" type="xsd:string" use="required" /> 

      <xsd:attribute name="value" type="xsd:string" use="required" /> 

</xsd:schema>

第二步 建立規則 xml檔案

<beans xmlns="http://3g.ahong.com/schema/mrp/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 

  xsi:schemalocation="http://3g.ahong.com/schema/xls/beans xlsbean.xsd"> 

<!--filename支援通配符--> 

<bean classname="com.ivfly.xlsbean.test.bean" filename="*測試*.xls" head="4" from="5"> 

  <!-- bean屬性名,xls中列名 順序不要求 --> 

  <property name="id" value="編号" nullable="true" seq="0" type="java.lang.string"></property> 

  <property name="name" value="使用者名" nullable="true" seq="1" type="java.lang.string"></property> 

  <property name="qq" value="qq号" seq="2" type="java.lang.string"></property> 

  <property name="age" value="年齡" seq="3" type="java.lang.string" formular="age" warringformat="%s 資料格式不正确"></property> 

  <property name="birthdate" value="出生日期" seq="4" type="java.util.date"></property> 

</bean> 

</beans>

重點介紹一下property節點。

其中: formular 為驗證規則;warringformat 為警告格式化串。 name 對應 bean值對象的屬性名,value為excel的列頭。 其他屬性,暫時沒有維護。

第三步。解析xml 并将配置資訊初始化為對象。

beansspecificationutil.java  主要為配置映射解釋器

package com.ivfly.xlsbean; 

import java.io.ioexception; 

import java.io.inputstream; 

import org.apache.commons.digester.digester; 

import org.xml.sax.saxexception; 

/** 

* 擷取映射配置 

*/ 

public class beansspecificationutil { 

  /** 

    * 解析xml,擷取跟元素節點 

    * @param xml,包路徑 

    * @return 

    */ 

  public static beansspecification getbeans (inputstream xml) { 

    digester digester = new digester();     

    //當遇到<beans>時建立一個com.beansspecificationimpl對象,并将其放在棧頂 

    digester.addobjectcreate("beans", "com.ivfly.xlsbean.beansspecification"); 

    //根據<beans>元素的屬性(attribute),對剛建立的com.beansspecificationimpl對象的屬性(property)進行設定 

    digester.addsetproperties("beans"); 

    //當遇到<beans>的子元素<bean>時建立一個com.beanspecificationimpl對象,并将其放在棧頂。 

    digester.addobjectcreate("beans/bean", "com.ivfly.xlsbean.beanspecification"); 

    // 

    digester.addsetproperties("beans/bean"); 

    //将調用beans級即beansspecificationimpl的addbean方法,參數為beanspecificationimpl(棧頂元素) 

    digester.addsetnext("beans/bean", "addbean"); 

    //同樣遇到<property>的時候執行個體化一個com.propertyspecificationimpl對象 

    digester.addobjectcreate("*/property", "com.ivfly.xlsbean.propertyspecification"); 

    //指派 

    digester.addsetproperties("*/property"); 

    //調用第二棧頂元素的addproperty方法 

    digester.addsetnext("*/property", "addproperty"); 

    //所有的配置都将裝在這裡 

    beansspecification beans=null; 

    try { 

      //解析完成後傳回根元素 

      beans = (beansspecification) digester.parse(xml); 

    } catch (ioexception e) { 

      e.printstacktrace(); 

    } catch (saxexception e) { 

    } 

    return beans; 

  }