项目笔记:创建XML文件和导出功能
一、创建XML文件:
要生成的XML结构:
//创建文件夹
private void createFilePah(String path){
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
}
@Override
public void createXML() {
try{
Integer softLicBType = 0;
Integer softLicWType = 1;//类型,判断来创建正版还是非正版
//根据Type来获取非正版数据List
List<GenuineSerialNumberManagement> softLicB = genuineSerialNumberManagementDao.queryByType(softLicBType);
String pathB = ReadConfigFileUtil.getValue(SystemStatics.ROOTDIR) + SystemStatics.PATH_PAW + File.separator+"genuineSerialNumberManagement";
createFilePah(pathB);//判断文件夹路径是否存,不存在就创建文件夹
String XMLPathB =pathB +File.separator + "SoftLicB.xml";//组合非正版文件路径
List<GenuineSerialNumberManagement> softLicW = genuineSerialNumberManagementDao.queryByType(softLicWType);
String pathW = ReadConfigFileUtil.getValue(SystemStatics.ROOTDIR) + SystemStatics.PATH_PAW + File.separator+"genuineSerialNumberManagement";
createFilePah(pathW);
String XMLPathW = pathW+File.separator + "SoftLicW.xml";
Document documentB = DocumentHelper.createDocument();//创建文件
Element rootB = documentB.addElement("SoftLicB");//创建文件根目录
List<Integer> ss = new ArrayList<Integer>();//声明一个空List用于保存softId,当softId已经写过一次时,就不再写而是写它下级的东西
for (GenuineSerialNumberManagement type : softLicB){
if(!ss.contains(type.getSoftId())){//如果不存在就增加进去
ss.add(type.getSoftId());
Element softWare = rootB.addElement("Software");//二级目录,二级目录里面有SoftId和DisplayName
softWare.addAttribute("SoftId", type.getSoftId().toString());
softWare.addAttribute("DisplayName", type.getSoftDisplayName());
//根据type和softId来获取数据list,因为同一个softId下面的东西需要在三级目录上
List<GenuineSerialNumberManagement> softIdB = genuineSerialNumberManagementDao.queryBySoftIdAndType(type.getSoftId(),softLicBType);
for(GenuineSerialNumberManagement soft : softIdB){
Element license = softWare.addElement("license");//三级目录,三级目录里面有Version和value
license.addAttribute("Version", soft.getVersion());
license.addAttribute("value", soft.getSerialNumber());
}
}else{
continue;
}
}
XMLUtil.writeXml(documentB, XMLPathB);//在路径下写入文件
Document documentW = DocumentHelper.createDocument();
Element rootW = documentW.addElement("SoftLicW");
List<Integer> ww = new ArrayList<Integer>();
for(GenuineSerialNumberManagement type : softLicW){
if(!ww.contains(type.getSoftId())){
ww.add(type.getSoftId());
Element softWare = rootW.addElement("Software");
softWare.addAttribute("SoftId", type.getSoftId().toString());
softWare.addAttribute("DisplayName", type.getSoftDisplayName());
List<GenuineSerialNumberManagement> softIdW = genuineSerialNumberManagementDao.queryBySoftIdAndType(type.getSoftId(), softLicWType);
for(GenuineSerialNumberManagement soft : softIdW){
Element license = softWare.addElement("license");
license.addAttribute("Version", soft.getVersion());
license.addAttribute("value", soft.getSerialNumber());
}
}else{
continue;
}
}
XMLUtil.writeXml(documentW, XMLPathW);
}catch(Exception e){
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
我们看下与数据库交互的queryBySoftIdAndType
@SuppressWarnings("unchecked")
@Override
public List<GenuineSerialNumberManagement> queryBySoftIdAndType(Integer softId,Integer type) {
return this.getSession().createQuery(" FROM " + this.clazz.getName() + " this WHERE this.softId =(:softId) AND this.type =(:type)")
.setParameter("softId", softId)
.setParameter("type", type)
.list();
}
然后调用该方法即可生成XML
//自动生成xml
genuineSerialNumberManagementService.createXML();
二、导出功能:
<form id="export_serialNumber" action="" style="padding: 0; margin: 0;" method="post" target="_blank">
<input type="hidden" id="exportType" name="genuineSerialNumberManagement.type" value="" />
</form>
//导出规则库
function exportSerialNumber(){
var currentType = $("input[name='serialNumber']:checked").val();
if (currentType == "") {
showMsg("系统提示", "请选择要导出的类型!", false);
return;
}
if ($.checkSession()) {
var $form = $("#export_serialNumber");
var action = "";
try {
$("#exportType").val(currentType);
action = $form.attr("action");
$form.attr("action", "${basePath}/genuineSerialNumberManagementAction_export.do").submit();
} finally {
$form.attr("action", action);
}
}
}
注意form表单里需要加个input隐藏域用于传type值
//导出规则库
public String export() {
log.info("【正版序列号管理--导出规则库:开始】");
try {
if (genuineSerialNumberManagement != null) {
String filePath = ReadConfigFileUtil.getValue(SystemStatics.ROOTDIR) + SystemStatics.PATH_PAW + File.separator+"genuineSerialNumberManagement\\";
// 为下载 做 准备
String fileName = null;
if(genuineSerialNumberManagement.getType()!=null && genuineSerialNumberManagement.getType() == 0){
fileName = "SoftLicB.xml";
}
if(genuineSerialNumberManagement.getType()!=null && genuineSerialNumberManagement.getType() == 1){
fileName = "SoftLicW.xml";
}
ActionContext.getContext().put("fileName", fileName);
ActionContext.getContext().put("filePath", filePath + fileName);
}
log.info("【正版序列号管理--导出规则库:结束】");
return RETURN_DOWNLOADUI;
} catch (Exception e) {
log.error("【正版序列号管理--导出规则库:失败】", e);
return null;
}
}