定义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 >
< 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 >
< 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;
}
}
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;
}
}
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/756528,如需转载请自行联系原作者