使用dom4j解析XML文件(3)

1.源代码

package com.zx.str;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.zx.exception.ExcelConfigException;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <pre>
* 功能描述:EXCEL配置信息存储类
*
* Author : penghuaiyi
* Date : 2007-11-14
* Time : 09:22:19
* Version: 1.2
* </pre>
*/
public class ExcelMappingConfig
{
private final InputStream is; //输入流
private final String configFileName;//配置文件名称

private Map<String,Object> head=new HashMap<String,Object>();//EXCEL表头
private String className; //EXCEL文件对应类的配置信息
private String sheetNameField; //EXCEL文件工作表名称对应JAVA字段的配置信息
private Map<String,Object> title=new HashMap<String,Object>(); //EXCEL文件标题的配置信息
private List<Map<String,Object>> cells = new ArrayList<Map<String,Object>>(); //EXCEL文件数据项对应JAVA字段的配置信息
private Map<String,Object> data = new HashMap<String,Object>(); //EXCEL文件数据的配置信息

/**
* <pre>
* 设置EXCEL配置文件
*
* @param configFilePath EXCEL配置文件路径
* 以"/"开始的,是相对于classpath根目录的路径;
* 否则相对于本文件所在目录的路径
*
* @throws ExcelConfigException 配置文件异常
* </pre>
*/
public ExcelMappingConfig(String configFilePath) throws ExcelConfigException
{
if(configFilePath==null)
{
throw new ExcelConfigException("没有初始化EXCEL配置文件!");
}
try
{
is=getClass().getResourceAsStream(configFilePath);
}
catch (Exception e)
{
throw new ExcelConfigException("没有找到EXCEL配置文件!");
}
configFileName=configFilePath;
init();
}

/**
* <pre>
* 设置EXCEL配置文件
*
* @param configFile EXCEL配置文件
*
* @throws ExcelConfigException 配置文件异常
* </pre>
*/
public ExcelMappingConfig(File configFile) throws ExcelConfigException
{
if(configFile==null)
{
throw new ExcelConfigException("没有初始化EXCEL配置文件!");
}
try
{
is=new FileInputStream(configFile);
}
catch (Exception e)
{
throw new ExcelConfigException("没有找到EXCEL配置文件!");
}
configFileName=configFile.getName();
init();
}

public Map<String, Object> getHead()
{
return head;
}

public String getClassName()
{
return className;
}

public String getSheetNameField()
{
return sheetNameField;
}

public Map<String, Object> getTitle()
{
return title;
}

public List<Map<String,Object>> getCells()
{
return cells;
}

public Map<String,Object> getData()
{
return data;
}

/**
* <pre>
* 解析EXCEL配置文件
*
* @throws ExcelConfigException 配置文件异常
* </pre>
*/
private void init() throws ExcelConfigException
{
SAXReader saxReader = new SAXReader();
Document doc;
try
{
doc = saxReader.read(is);
}
catch (Exception e)
{
throw new ExcelConfigException("无法解析配置文件"+configFileName+"!");
}

/*读取head*/
Element headElement = (Element)doc.selectSingleNode("excel-mapping-config/head");
if (headElement != null)
{
setMapIntegerByElementAttribute(head, headElement, "height",true);

headElement = (Element)doc.selectSingleNode("excel-mapping-config/head/merge");
if(headElement ==null)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中没有找到excel-mapping-config/head/merge结点!");
}
setMapIntegerByElementAttribute(head, headElement, "beginColumn",false);
setMapIntegerByElementAttribute(head, headElement, "beginRow",false);
setMapIntegerByElementAttribute(head, headElement, "endColumn",false);
setMapIntegerByElementAttribute(head, headElement, "endRow",false);
}

/*读取className*/
Element classNameElement = (Element)doc.selectSingleNode("excel-mapping-config/className");
if (classNameElement == null)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中没有找到excel-mapping-config/className结点!");
}
className=classNameElement.getTextTrim();
if(className==null||className.length()==0)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中结点excel-mapping-config/className没有设置内容!");
}

/*读取sheet*/
Element sheetNode = (Element)doc.selectSingleNode("excel-mapping-config/sheet");
if(sheetNode==null)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中没有找到excel-mapping-config/sheet结点!");
}
sheetNameField=sheetNode.attributeValue("nameField");
if(sheetNameField==null||sheetNameField.length()==0)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中结点excel-mapping-config/sheet结点中的nameField属性没有设置内容!");
}

/*读取title*/
Element titleElement = (Element) doc.selectSingleNode("excel-mapping-config/sheet/title");
if (titleElement == null)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中没有找到excel-mapping-config/sheet/title结点!");
}
setMapIntegerByElementAttribute(title, titleElement, "row",false);
setMapIntegerByElementAttribute(title, titleElement, "height",true);
setMapBooleanByElementAttribute(title, titleElement, "freeze",true);
setMapBooleanByElementAttribute(title, titleElement, "checkTitleName",true);

/*读取cell*/
List cellList = doc.selectNodes("excel-mapping-config/sheet/title/cell");
if(cellList==null)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中没有找到excel-mapping-config/sheet/title/cell结点!");
}
for(Object obj:cellList)
{
Element cellElement=(Element)obj;
Map<String,Object> cellMap=new HashMap<String,Object>();
setMapIntegerByElementAttribute(cellMap, cellElement, "column",false);
setMapStringByElementAttribute(cellMap, cellElement, "field",false);
setMapIntegerByElementAttribute(cellMap, cellElement, "maxLength",true);
setMapStringByElementAttribute(cellMap, cellElement, "type",true);
setMapStringByElementAttribute(cellMap, cellElement, "format",true);
setMapIntegerByElementAttribute(cellMap, cellElement, "width",true);
cellMap.put("titleName",cellElement.getTextTrim());
cells.add(cellMap);
}

/*读取data*/
Element dataElement = (Element) doc.selectSingleNode("excel-mapping-config/sheet/data");
if (dataElement == null)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中没有找到excel-mapping-config/sheet/data结点!");
}
setMapIntegerByElementAttribute(data, dataElement, "begin",false);
setMapIntegerByElementAttribute(data, dataElement, "end",true);
setMapIntegerByElementAttribute(data, dataElement, "height",true);
}

/**
* <pre>
* 取得EXCEL配置文件的一个结点元素的属性信息,校验它是否为整数,并存放在MAP中
*
* @param map 存储EXCEL配置文件信息
* @param element EXCEL配置文件的一个结点元素
* @param attributeName EXCEL配置文件的一个结点元素的属性名
* @param isOptional EXCEL配置文件的一个结点元素的属性是否为可选顶
*
* @throws ExcelConfigException 配置文件异常
* </pre>
*/
private void setMapIntegerByElementAttribute(Map<String,Object> map, Element element, String attributeName,boolean isOptional) throws ExcelConfigException
{
String attributeValue = element.attributeValue(attributeName);
if (attributeValue == null || attributeValue.trim().length() ==0)
{
if(!isOptional)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中结点"+element.getName()+"的属性"+attributeName+"没有设置!");
}
}
else
{
try
{
map.put(attributeName, Integer.parseInt(attributeValue.trim()));
}
catch (NumberFormatException nfe)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中结点"+element.getName()+"的属性"+attributeName+"不是Integer类型!");
}
}
}

/**
* <pre>
* 取得EXCEL配置文件的一个结点元素的属性信息,并存放在MAP中
*
* @param map 存储EXCEL配置文件信息
* @param element EXCEL配置文件的一个结点元素
* @param attributeName EXCEL配置文件的一个结点元素的属性名
* @param isOptional EXCEL配置文件的一个结点元素的属性是否为可选顶
*
* @throws ExcelConfigException 配置文件异常
* </pre>
*/
private void setMapStringByElementAttribute(Map<String,Object> map, Element element, String attributeName,boolean isOptional) throws ExcelConfigException
{
String attributeValue = element.attributeValue(attributeName);
if (attributeValue == null || attributeValue.trim().length() ==0)
{
if(!isOptional)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中结点"+element.getName()+"的属性"+attributeName+"没有设置!");
}
}
else
{
map.put(attributeName,attributeValue.trim());
}
}

/**
* <pre>
* 取得EXCEL配置文件的一个结点元素的属性信息,校验它是否为Boolean,并存放在MAP中
*
* @param map 存储EXCEL配置文件信息
* @param element EXCEL配置文件的一个结点元素
* @param attributeName EXCEL配置文件的一个结点元素的属性名
* @param isOptional EXCEL配置文件的一个结点元素的属性是否为可选顶
*
* @throws ExcelConfigException 配置文件异常
* </pre>
*/
private void setMapBooleanByElementAttribute(Map<String,Object> map, Element element, String attributeName,boolean isOptional) throws ExcelConfigException
{
String attributeValue = element.attributeValue(attributeName);
if (attributeValue == null || attributeValue.trim().length() ==0)
{
if(!isOptional)
{
throw new ExcelConfigException("在配置文件" + configFileName + "中结点"+element.getName()+"的属性"+attributeName+"没有设置!");
}
}
else
{
map.put(attributeName, Boolean.valueOf((attributeValue.trim())));
}
}
}




2.要解析的xml文件

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

<!--Excel文件格式配置信息-->
<excel-mapping-config>
<!--
EXCEL表头,仅用于导出时(可选项)

属性 说明
height :EXCEL头单元格高度(可选项)
-->
<head height="600">
<!--
合并EXCEL表头单元格,若设置头部,则为必选项

属性 说明
beginColumn :合并起始列位置索引(从0开始计数)
beginRow :合并起始行位置索引(从0开始计数)
endColumn :合并终止列位置索引(从0开始计数)
endRow :合并终止行位置索引(从0开始计数)
-->
<merge beginColumn="0" beginRow="0" endColumn="7" endRow="0"/>
</head>

<!--
与EXCEL文件数据对应的JAVA类

说明: 1. 该类必须提供公有的默认构造方法
2. 映射字段对应成员必须提供公有的set方法和get方法
3. 映射字段对应成员类型可以为字符类型(java.lang.String),
数字类型(java.lang.Double),
日期类型(java.util.Date)
4.导入时建议实现接口com.iman.lts.common.excel.entity.IBaseExcel
-->
<className>com.iman.common.excel.example.Example</className>

<!--
EXCEL文件工作表配置信息

属性 说明
nameField :工作表名称所对应JAVA类的字段名称
-->
<sheet nameField="sheetName">
<!--
EXCEL文件标题配置信息

属性 说明
row :标题行位置索引(从0开始计数)
height :标题单元格高度,仅用于导出时(可选项)
freeze :是否冻结{true,false},默认false,仅用于导出时(可选项)
checkTitleName:是否校验标题名称{true,false},默认false,仅用于导入时(可选项)
-->
<title row="1" height="400" freeze="true" checkTitleName="false">
<!--
EXCEL文件数据元素配置信息

属性 说明
column :列位置索引,从0开始计数
field :数据元素所对应JAVA类的字段名称
type :数据类型{Double,Date,String},默认String(可选项)
format :数据格式,只可用于Double和Date类型(可选项)
maxLength:数据元素的最大长度,用于String类型,默认全部长度;若超过最大长度,则截取到最大长度,仅用于导入时(可选项)
width :数据元素单元格宽度,仅用于导出时(可选项)
-->
<cell column="0" field="id" type="Double" format="#">编号</cell>
<cell column="1" field="name" width="15">姓名</cell>
<cell column="2" field="age" type="Double" format="#">年龄</cell>
<cell column="3" field="birthday" type="Date" format="yyyy-MM-dd" width="15">出生日期</cell>
<cell column="4" field="salary" type="Double" format="#.00" width="20">薪水</cell>
<cell column="5" field="bonus" type="Double" format="#.00" width="20">奖金</cell>
<cell column="6" field="statDate" type="Date" format="yyyy-MM-dd HH:mm:ss" width="25">统计时间</cell>
<cell column="7" field="remark" maxLength="200" width="50">备注</cell>
</title>
<!--
数据属性配置信息

属性 说明
begin :数据元素的起始行,从0开始计数
end :数据元素的结束行,从0开始计数,默认到最后一行,仅用于导入时(可选项)
height :数据元素单元格高度,仅用于导出时(可选项)
-->
<data begin="2" end="" height="300"/>
</sheet>
</excel-mapping-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值