根据XML配置规则导入Excel数据(二)定义XML规则对象

简介:
规则设计到三个对象。工具将XML 分三级解析。
 
              类
                      对应
xml节点
作用                                                                    
 BeansSpecification  beans    表示一个根节点,包含所有Bean定义信息。
 BeanSpecification  bean  代表bean的配置信息,包含多个propertys
 PropertySpecification  property  代表bean 的属性配置信息
 
BeansSpecification.java
package com.ivfly.xlsbean; 


import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* 1.去掉了父类 
* 2.添加单例 
*                 根元素 
*/
 
public  class BeansSpecification{ 
   private  static BeansSpecification instance=  null
   public  static BeansSpecification getInstance(InputStream xmlStream){ 
     if(instance==  null){ 
      instance = BeansSpecificationUtil.getBeans(xmlStream); 
    } 
     return instance; 
  } 
   /** 
    * 跟元素下所有的beans 
    */
 
   private List<BeanSpecification> beans =  new ArrayList<BeanSpecification>(); 

   public List<BeanSpecification> getBeans() { 
     return  this.beans; 
  } 

   public  void addBean(BeanSpecification field) { 
     this.beans.add(field); 
  } 

   /** 
    * 根据文件名和类名查找 
    * @param fileName 
    * @return 
    */
 
   public BeanSpecification getBean(String fileName) { 
     for (BeanSpecification bean : beans) { 
       if (FileUtil.wildcard(bean.getFileName(), fileName)||bean.getClassName().equals(fileName)) { 
         return bean; 
      } 
    } 
     return  null
  } 
   public BeanSpecification getBeanByClassName(String className) { 
     for (BeanSpecification bean : beans) { 
       if (bean.getClassName().equals(className)) { 
         return bean; 
      } 
    } 
     return  null
  } 


 
BeanSpecification.java
package com.ivfly.xlsbean; 


import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.LinkedHashMap; 
import java.util.List; 
import java.util.Map; 

import org.apache.commons.beanutils.PropertyUtils; 


/** 
*    
*                 描述实体属性 
*/
 
public  class BeanSpecification{ 

   private Map<String,PropertySpecification> properties =  new HashMap<String,PropertySpecification>(); 

   /** 
    * 类路径名 
    */
 
   private String className; 
   /** 
    * 描述对应Class的properties 
    */
 
   private Map    describer; 
   /** 
    * 文件名 
    */
 
   private String fileName; 
   /** 
    * excel中数据记录的起点行数 
    */
 
   private Integer from; 

   /** 
    * 列标题所在行 
    */
 
   private Integer head; 

   /** 
    * 类属性和excel列名对应键值对 
    */
 
   private Map<String, String> nv= new LinkedHashMap<String,String>(); 
    
   /** 
    * 类属性和excel列名对应键值对,反过来 
    */
 
   private Map<String, String> vn= new HashMap<String,String>(); 
    
   private List<String> propertyList =  new ArrayList<String>(); 

   public  void addProperty(PropertySpecification field) { 
     this.nv.put(field.getName(), field.getValue()); 
     this.vn.put(field.getValue(),field.getName()); 
     this.properties.put(field.getName(), field); 
    propertyList.add(field.getName()); 
  } 

    
   public List<String> getPropertyList() { 
     return propertyList; 
  } 


   public String getClassName() { 
     return  this.className; 
  } 

   public  void setClassName(String classname) { 
     this.className = classname; 
  } 

   public Map<String,PropertySpecification> getProperty() { 
     return properties; 
  } 

   public String getPropertyNameByValue(String value) { 
     return vn.get(value); 
  } 
    
   public  boolean nullable(String propertyName){ 
     if( this.properties.containsKey(propertyName)){ 
       return  this.properties.get(propertyName).isNullable(); 
    } else  return  false
  } 

   public  void setProperty(Map<String,PropertySpecification> property) { 
     this.properties = property; 
  } 

   public String getFileName() { 
     return fileName; 
  } 

   public  void setFileName(String fileName) { 
     this.fileName = fileName; 
  } 

   public Integer getFrom() { 
     return from; 
  } 

   public  void setFrom(Integer from) { 
     this.from = from; 
  } 

   public Integer getHead() { 
     return head; 
  } 

   public  void setHead(Integer head) { 
     this.head = head; 
  } 

   public Map<String, String> getNv() { 
     return nv; 
  } 

   public  void setNv(Map<String, String> nv) { 
     this.nv = nv; 
  } 


  @SuppressWarnings( "unchecked"
   public Map getDescriber() { 
    describer =  new HashMap(); 
     try { 
      Class cls = Class.forName( this.className); 
      Object bean = cls.newInstance(); 
      describer = PropertyUtils.describe(bean); 
    }  catch (Exception e) { 
      e.printStackTrace(); 
    } 
     return describer; 
  } 

    

 
PropertySpecification.java
package com.ivfly.xlsbean; 
/** 
*    
* 描述属性的映射 

*/
 
public  class PropertySpecification { 
   /** 
    * 顺序号 没有维护 
    */
 
   private  int    seq; 
   /** 
    * 格式 
    */
 
   private String formular; 
   /** 
    * 提示格式 
    */
 
   private String warringFormat; 
    
   private    String type; 
   /** 
    * 属性名 
    */
 
   private String name; 
    
   /** 
    * 列名 
    */
 
   private String value; 
    
   /** 
    * 是否可以为空 
    */
 
   private  boolean nullable= true
    
   private  int index; 

   public String getName() { 
     return name; 
  } 

   public  void setName(String name) { 
     this.name = name; 
  } 

   public String getValue() { 
     return value; 
  } 

   public  void setValue(String value) { 
     this.value = value; 
  } 

   public  int getIndex() { 
     return index; 
  } 

   public  void setIndex( int index) { 
     this.index = index; 
  } 
    
    
   public  boolean isNullable() { 
     return nullable; 
  } 

   public  void setNullable( boolean nullable) { 
     this.nullable = nullable; 
  } 

  @Override 
   public String toString() { 
     return  "name="+name+ ";value="+value+ ";seq="+seq+ ";type="+type+ ",nullable="+nullable; 
  } 

   public  int getSeq() { 
     return seq; 
  } 

   public  void setSeq( int seq) { 
     this.seq = seq; 
  } 

   public String getType() { 
     return type; 
  } 

   public  void setType(String type) { 
     this.type = type; 
  } 

   public String getFormular() { 
     return formular; 
  } 

   public  void setFormular(String formular) { 
     this.formular = formular; 
  } 

   public String getWarringFormat() { 
     return warringFormat; 
  } 

   public  void setWarringFormat(String warringFormat) { 
     this.warringFormat = warringFormat; 
  } 

 


本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/756533,如需转载请自行联系原作者
相关文章
|
4月前
|
Python
如何根据Excel某列数据为依据分成一个新的工作表
在处理Excel数据时,我们常需要根据列值将数据分到不同的工作表或文件中。本文通过Python和VBA两种方法实现该操作:使用Python的`pandas`库按年级拆分为多个文件,再通过VBA宏按班级生成新的工作表,帮助高效整理复杂数据。
|
4月前
|
数据采集 数据可视化 数据挖掘
用 Excel+Power Query 做电商数据分析:从 “每天加班整理数据” 到 “一键生成报表” 的配置教程
在电商运营中,数据是增长的关键驱动力。然而,传统的手工数据处理方式效率低下,耗费大量时间且易出错。本文介绍如何利用 Excel 中的 Power Query 工具,自动化完成电商数据的采集、清洗与分析,大幅提升数据处理效率。通过某美妆电商的实战案例,详细拆解从多平台数据整合到可视化报表生成的全流程,帮助电商从业者摆脱繁琐操作,聚焦业务增长,实现数据驱动的高效运营。
|
6月前
|
存储 安全 大数据
网安工程师必看!AiPy解决fscan扫描数据整理难题—多种信息快速分拣+Excel结构化存储方案
作为一名安全测试工程师,分析fscan扫描结果曾是繁琐的手动活:从海量日志中提取开放端口、漏洞信息和主机数据,耗时又易错。但现在,借助AiPy开发的GUI解析工具,只需喝杯奶茶的时间,即可将[PORT]、[SERVICE]、[VULN]、[HOST]等关键信息智能分类,并生成三份清晰的Excel报表。告别手动整理,大幅提升效率!在安全行业,工具党正碾压手动党。掌握AiPy,把时间留给真正的攻防实战!官网链接:https://www.aipyaipy.com,解锁更多用法!
|
4月前
|
Python
将Excel特定某列数据删除
将Excel特定某列数据删除
|
9月前
|
分布式计算 Hadoop 大数据
从Excel到Hadoop:数据规模的进化之路
从Excel到Hadoop:数据规模的进化之路
192 10
|
11月前
|
存储 Java easyexcel
招行面试:100万级别数据的Excel,如何秒级导入到数据库?
本文由40岁老架构师尼恩撰写,分享了应对招商银行Java后端面试绝命12题的经验。文章详细介绍了如何通过系统化准备,在面试中展示强大的技术实力。针对百万级数据的Excel导入难题,尼恩推荐使用阿里巴巴开源的EasyExcel框架,并结合高性能分片读取、Disruptor队列缓冲和高并发批量写入的架构方案,实现高效的数据处理。此外,文章还提供了完整的代码示例和配置说明,帮助读者快速掌握相关技能。建议读者参考《尼恩Java面试宝典PDF》进行系统化刷题,提升面试竞争力。关注公众号【技术自由圈】可获取更多技术资源和指导。
|
11月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
344 6
|
6月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
159 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
263 1
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)

热门文章

最新文章

相关课程

更多