根据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,如需转载请自行联系原作者
相关文章
|
10月前
|
XML 数据采集 API
用Lxml高效解析XML格式数据:以天气API为例
免费Python教程:实战解析中国天气网XML数据,详解Lxml库高效解析技巧、XPath用法、流式处理大文件及IP封禁应对策略,助你构建稳定数据采集系统。
452 0
|
XML 存储 JSON
Twaver-HTML5基础学习(19)数据容器(2)_数据序列化_XML、Json
本文介绍了Twaver HTML5中的数据序列化,包括XML和JSON格式的序列化与反序列化方法。文章通过示例代码展示了如何将DataBox中的数据序列化为XML和JSON字符串,以及如何从这些字符串中反序列化数据,重建DataBox中的对象。此外,还提到了用户自定义属性的序列化注册方法。
313 1
|
XML Web App开发 数据格式
HTML 页面显示 XML 数据
10月更文挑战第2天
|
XML JSON 前端开发
C#使用HttpClient四种请求数据格式:json、表单数据、文件上传、xml格式
C#使用HttpClient四种请求数据格式:json、表单数据、文件上传、xml格式
3532 0
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
415 1
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
430 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
Java Maven
maven项目的pom.xml文件常用标签使用介绍
第四届人文,智慧教育与服务管理国际学术会议(HWESM 2025) 2025 4th International Conference on Humanities, Wisdom Education and Service Management
1377 8
|
XML JavaScript Java
java与XML文件的读写
java与XML文件的读写
273 3

相关课程

更多