根据XML配置规则导入Excel数据(一)定义XML规则

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dba10g.blog.51cto.com/764602/756528
 
定义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:complexType > 
       < xsd:sequence > 
         < xsd:element  ref ="property"  minOccurs ="1"  maxOccurs ="unbounded" /> 
       </ xsd:sequence > 
       < 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:complexType > 
   </ xsd:element > 
   < xsd:element  name ="property" > 
     < xsd:complexType > 
       < xsd:attribute  name ="name"  type ="xsd:string"  use ="required"  /> 
       < xsd:attribute  name ="value"  type ="xsd:string"  use ="required"  /> 
     </ xsd:complexType > 
   </ xsd:element > 

</ xsd:schema >
 
 
第二步 建立规则 XML文件
 
<? xml  version ="1.0"  encoding ="UTF-8" ?> 
< 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) { 
      e.printStackTrace(); 
    } 
     
     return beans; 
  } 

本文出自 “简单” 博客,请务必保留此出处http://dba10g.blog.51cto.com/764602/756528

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值