前台代码:

//导出一条Xml
function btn_createXml(){
var title =$("#editButton").attr("title") ;
var keyValue = $("#gridTable").jqGridRowValue("id");
if (checkedArray(keyValue)) {
var newAction ='/log/windowsSafetyBaseLineLogAction_createXml.do?ids='+keyValue+'&jsonStr='+jsonStr;
createFun( newAction );
}
}
//导出全部xml
function btn_createXmlAll(){
var newAction = '/log/windowsSafetyBaseLineLogAction_createXmlAll.do?jsonStr='+jsonStr;
createFun( newAction );
} function createFun( newAction ){
var ids=$("#gridTable").getDataIDs();
if(true){
var $form = $("#queryListUI_form") ;
var action = "";
try {
action = $form.attr("action");
$form.attr("action","${basePath}"+newAction).submit();
}finally{
$form.attr("action",action);
}
}
}

  后台代码:

//导出全部XML
private String downAll; public void createXmlAll() {
downAll = "";
createXml();
} //导出xml功能
public void createXml(){
String ESswitch = getSession().getAttribute("ES").toString();
if("".equals(ESswitch)){
//targetEScreateXml();
}else{
targetcreateXml();
}
} public void targetcreateXml(){
log.info("导出Xml开始-BEGIN");
Page<WindowsSafetyBaseLineLog> page = null;
List<WindowsSafetyBaseLineLog> dataList = null;
// 构建路径
String downLoadPath = "/WEB-INF/download/log/";
String rootPath = getServletContext().getRealPath(downLoadPath); try {
filterQuery();
@SuppressWarnings("unchecked")
Map<String, List<String>> mgrOrgMap = (Map<String, List<String>>) getSession().getAttribute(OrganizationUtil.ORG_MGR);
//导出全部XML
if (downAll != null && downAll.equals("")) {
if(OrganizationUtil.isNullMgrOrg(mgrOrgMap)){
dataList = new ArrayList<WindowsSafetyBaseLineLog>();
}else{
queryBean.setSession(getSession());
page = windowsSafetyBaseLineLogService.query(queryBean, , getPageSize());
dataList = page.getDataList();
dataList = windowsSafetyBaseLineLogService.queryAll();
dataList= getAllOrgDataList(dataList);
}
} else { //导出单个XML
if (null != getRequest().getParameter("ids")) {
dataList = new ArrayList<WindowsSafetyBaseLineLog>();
String ids[] = getRequest().getParameter("ids").split(",");
for (int j = ; j < ids.length; j++) {
WindowsSafetyBaseLineLog windowsSafetyBaseLineLog = windowsSafetyBaseLineLogService.queryById(ids[j]);
if (null != windowsSafetyBaseLineLog) {
dataList.add(windowsSafetyBaseLineLog);
dataList= getAllOrgDataList(dataList); //获取组织机构的方法
}
}
}
}
JsonConfigEx jsonConfigEx = new JsonConfigEx();
String json = JsonUtils.collection2Json(dataList,jsonConfigEx);
JSONArray jsonArr=JSONArray.fromObject(json);
addWordByJson(jsonArr);
Document document = createDocumentByJson(jsonArr);
String targetfile = rootPath + System.getProperty("file.separator") + "windowsSafetyBaseLineLog" + ".xml";
FileOutputStream fw = new FileOutputStream(targetfile);
//设置文件编码
OutputFormat xmlFormat = new OutputFormat();
xmlFormat.setEncoding("UTF-8");
// 设置换行
xmlFormat.setNewlines(true);
// 生成缩进
xmlFormat.setIndent(true);
// 使用4个空格进行缩进, 可以兼容文本编辑器
xmlFormat.setIndent(" ");
XMLWriter xmlWriter = new XMLWriter(fw,xmlFormat);
//写入文件
xmlWriter.write(document);
//关闭
xmlWriter.close();
String fileName="windowsSafetyBaseLineLog";
BaseActionUtill u=new BaseActionUtill();
u.downLoadFile(fileName, downLoadPath);
/* ServletActionContext.getRequest().setAttribute("downLoadPath",
downLoadPath);
ServletActionContext.getRequest().setAttribute("fileName",
"deviceOpenLog" + ".xml");*/
this.msg = RESULT_SUCCESS;
log.error("导出XML"+this.msg);
} catch (Exception e) {
e.printStackTrace();
log.error("导出XML失败");
}finally{
managerOperaterLogService.save("cems_createXML" ,new String[]{ "windowsSafetyBaseLineLog.xml"} );
}
log.info("导出Xml开始-END"); }

  其他需要用到的方法:公共配置项,创建xml文件,并写xml文件

public JSONArray addWordByJson(JSONArray json){
JSONArray jsonArrN=new JSONArray();
LogConfig logConfig =logConfigService.queryByTableName(tableName);//用于其他的一些公共的配置项写入数据
if(json.size()>){
for (int i = ; i < json.size(); i++) {
JSONObject jsonObj=json.getJSONObject(i);
if(logConfig!=null){
jsonObj.put("eventType", logConfig.getType().toString());
jsonObj.put("productType", logConfig.getProductType());
int type=logConfig.getRiskLevel();
RiskLevel riskLevel=riskLevelService.queryRiskLevelByType(type);
if(riskLevel!=null&& StringUtils.isNotBlank(riskLevel.getName())){
log.info("写入riskLevel一列-BEGIN");
String riskLevelName=MessageUtils.getMessage(riskLevel.getName());
jsonObj.put("riskLevel", riskLevelName);
log.info("写入riskLevel一列-END");
}
if(logConfig.getBehaviourType()!=null){
log.info("写入behaviourType一列-BEGIN");
int Type =logConfig.getBehaviourType();
String behaviourType= MessageUtils.getMessage(BehaviourType.getByState(Type).getResourceKey());
jsonObj.put("behaviourType", behaviourType);
log.info("写入behaviourType一列-END");
}
User user= userService.queryByAccount(jsonObj.getString("userAccount")) ;
if(user!=null){
String name= user.getName();
jsonObj.put("userName", name!=null?name:null); }
jsonArrN.add(jsonObj);
}else{
jsonObj.put("behaviourType", "");
jsonObj.put("riskLevel", "");
jsonObj.put("eventType", "");
jsonObj.put("productType", "");
User user= userService.queryByAccount(jsonObj.getString("userAccount")) ;
if(user!=null){
String name= user.getName();
jsonObj.put("userName", name!=null?name:null);
}
jsonArrN.add(jsonObj);
}
}
}
return jsonArrN;
}
//写入XML
public Document createDocumentByJson(JSONArray json){
//创建Document
Document document = DocumentHelper.createDocument();
try {
//获取列名
String jsonStr = new String(getRequest().getParameter("jsonStr").getBytes("ISO-8859-1"),"UTF-8");
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
RiskLevel rLevel = null; log.info("构建XML根节点-BEGIN");
Element logRoot = document.addElement("Log");
logRoot.addAttribute("tablename", tableName);
logRoot.addAttribute("Remark", MessageUtils.getMessage("sys.table.name.windowsSafetyBaseLineLog"));
log.info("构建XML根节点-END");
for (int i = ; i < json.size(); i++) {
JSONObject jsonObj=json.getJSONObject(i);
Element logIDElement = logRoot.addElement("LogID");
logIDElement.addAttribute("Remark", "ID");
logIDElement.setText(jsonObj.get("id")!=null ? jsonObj.get("id").toString() : ""); //事件主体
log.info("构建事件主体-BEGIN");
Element subjectElement = logRoot.addElement("Subject");
subjectElement.addAttribute("Remark", MessageUtils.getMessage("com.vrv.cems.log.eventSubject"));
log.info("构建事件主体-END");
log.info("写入主体数据-BEGIN"); Element ip = subjectElement.addElement("IP");
ip.addAttribute("Remark", jsonObject.getString("ip"));
ip.setText(jsonObj.get("ip")!=null ? jsonObj.get("ip").toString() : ""); /*Element mac = subjectElement.addElement("MAC");
mac.addAttribute("Remark", jsonObject.getString("mac"));
mac.setText(jsonObj.get("mac")!=null ? jsonObj.get("mac").toString() : "");*/ Element name = subjectElement.addElement("Name");
name.addAttribute("Remark", jsonObject.getString("name"));
name.setText(jsonObj.get("name")!=null ? jsonObj.get("name").toString() : ""); Element orgName = subjectElement.addElement("OrgName");
orgName.addAttribute("Remark", jsonObject.getString("orgName"));
orgName.setText(jsonObj.get("orgName")!=null ? jsonObj.get("orgName").toString() : ""); /*Element regUserAccount = subjectElement.addElement("RegUserAccount");
regUserAccount.addAttribute("Remark", jsonObject.getString("regUserAccount"));
regUserAccount.setText(jsonObj.get("regUserAccount")!=null ? jsonObj.get("regUserAccount").toString() : "");*/ Element userAccount = subjectElement.addElement("UserAccount");
userAccount.addAttribute("Remark", jsonObject.getString("userAccount"));
userAccount.setText(jsonObj.get("userAccount")!=null ? jsonObj.get("userAccount").toString() : ""); Element policyName = subjectElement.addElement("PolicyName");
policyName.addAttribute("Remark", jsonObject.getString("policyName"));
policyName.setText(jsonObj.get("policyName")!=null ? jsonObj.get("policyName").toString() : ""); Element templateName = subjectElement.addElement("TemplateName");
templateName.addAttribute("Remark", jsonObject.getString("templateName"));
templateName.setText(jsonObj.get("templateName")!=null ? jsonObj.get("templateName").toString() : ""); Element state = subjectElement.addElement("State");
state.addAttribute("Remark", jsonObject.getString("state"));
state.setText(jsonObj.get("state")!=null ? jsonObj.get("state").toString() : ""); Element ruleExcuteCount = subjectElement.addElement("RuleExcuteCount");
ruleExcuteCount.addAttribute("Remark", jsonObject.getString("ruleExcuteCount"));
ruleExcuteCount.setText(jsonObj.get("ruleExcuteCount")!=null ? jsonObj.get("ruleExcuteCount").toString() : ""); Element ruleFailCount = subjectElement.addElement("RuleFailCount");
ruleFailCount.addAttribute("Remark", jsonObject.getString("ruleFailCount"));
ruleFailCount.setText(jsonObj.get("ruleFailCount")!=null ? jsonObj.get("ruleFailCount").toString() : ""); log.info("写入主体数据-END");
//事件客体
log.info("构建事件客体-BEGIN");
Element objectElement = logRoot.addElement("Object");
objectElement.addAttribute("Remark", MessageUtils.getMessage("com.vrv.cems.log.eventObject")); log.info("构建事件客体-END");
log.info("写入客体数据-BEGIN"); //判断是否有其他公共的配置项
LogConfig logConfig =logConfigService.queryByTableName(tableName);
if (logConfig!=null) {
rLevel=riskLevelService.queryRiskLevelByType(logConfig.getRiskLevel()); Element riskLevel = objectElement.addElement("RiskLevel");
riskLevel.addAttribute("Remark", jsonObject.getString("riskLevel"));
riskLevel.setText(MessageUtils.getMessage(rLevel.getName())); Element behaviourType = objectElement.addElement("BehaviourType");
behaviourType.addAttribute("Remark", jsonObject.getString("behaviourType"));
behaviourType.setText(MessageUtils.getMessage(BehaviourType.getByState(logConfig.getBehaviourType()).getResourceKey())); Element eventType = objectElement.addElement("EventType");
eventType.addAttribute("Remark", jsonObject.getString("eventType"));
eventType.setText(jsonObj.get("eventType")!=null ? MessageUtils.getMessage(Type.getByState(jsonObj.getInt("eventType")).getResourceKey()) : "");
/*MessageUtils.getMessage(Type.getByState(s)) )(jsonObj.getInt("eventType").toString()) )*/
Element productType = objectElement.addElement("ProductType");
productType.addAttribute("Remark", jsonObject.getString("productType"));
productType.setText(jsonObj.get("productType")!=null ? MessageUtils.getMessage(ProductType.getByState(jsonObj.getString("productType")).getResourceKey()) : "");
} /*Element hardwareName = objectElement.addElement("auditDesc");
hardwareName.addAttribute("Remark", jsonObject.getString("auditDesc"));
hardwareName.setText(jsonObj.get("auditDesc")!=null ? jsonObj.get("auditDesc").toString() : ""); Element clientTime = objectElement.addElement("ClientTime");
clientTime.addAttribute("Remark", jsonObject.getString("clientTime"));
clientTime.setText((jsonObj.get("clientTime")!=null ? jsonObj.get("clientTime").toString() : "")); Element reportTime = objectElement.addElement("ReportTime");
reportTime.addAttribute("Remark", jsonObject.getString("reportTime"));
reportTime.setText((jsonObj.get("reportTime")!=null ? jsonObj.get("reportTime").toString() : "")); Element type= objectElement.addElement("Type");
type.addAttribute("Remark", jsonObject.getString("type"));
type.setText(jsonObj.get("type")!=null ? jsonObj.get("type").toString() : ""); Element state = objectElement.addElement("State");
state.addAttribute("Remark",jsonObject.getString("state"));
state.setText((jsonObj.get("state")!=null ? jsonObj.get("state") : "").toString());*/
log.info("写入客体数据-END"); }
} catch (Exception e) {
e.printStackTrace();
log.error("生成XML失败");
}
return document;
}

  关于上面的Document、Element,需要导入下面的包:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

  递归获取组织机构全路径方法

/*获取全路径组织机构*/
public List<WindowsSafetyBaseLineLog> getAllOrgDataList(List<WindowsSafetyBaseLineLog> dataList){
log.info("获取全路径组织机构-ST");
List<WindowsSafetyBaseLineLog> orgList = new ArrayList<WindowsSafetyBaseLineLog>();
if(dataList != null && dataList.size() > ){
for(WindowsSafetyBaseLineLog dspbpl : dataList){
WindowsSafetyBaseLineLog o = new WindowsSafetyBaseLineLog();
String orgName = dspbpl.getOrgName();
String name = getAname(dspbpl.getOrganization(), orgName);
BeanUtils.copyProperties(dspbpl, o);
o.setOrgName(name);
orgList.add(o);
}
}
return orgList;
}
/*递归获取全路径组织机构方法*/
private String getAname(Organization organization, String orgName) {
log.info("递归获取全路径组织机构方法-ST");
if( organization != null && organization.getParent()!=null){
String tName="";
tName=organization.getParent().getName()+"/"+orgName;
log.info("递归获取全路径组织机构方法-END");
return getAname(organization.getParent(), tName);
}else{
log.info("递归获取全路径组织机构方法-END");
return orgName;
}
}
// 公共查询过滤功能
private void filterQuery() {
// 拼排序条件。
String orderStr = getRequest().getParameter("order");
String sortStr = getRequest().getParameter("sort");
if (queryBean == null) {
queryBean = new WindowsSafetyBaseLineLogQueryBean();
}
if (sortStr != null && orderStr != null) {
String[] orderArr = orderStr.split(",");
String[] sortArr = sortStr.split(",");
for (int i = ; i < sortArr.length; i++) {
queryBean.addOrderProperty("this." + sortArr[i],
"asc".equals(orderArr[i]) ? Order.ASC : Order.DESC);
}
} }

导出XML效果:

项目笔记:导出XML和导出全部XML功能的更多相关文章

  1. Python导出Excel为Lua/Json/Xml实例教程(三):终极需求

    相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 Python导出E ...

  2. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  3. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  4. [SQLXML]FOR XML语法导出XML的易错之处

    原文:[SQLXML]FOR XML语法导出XML的易错之处 [SQLXML]FOR XML语法导出XML的易错之处 Version Date Creator Description 1.0.0.1 ...

  5. Power Designer导出实体类和NHibernate xml文件

    Power Designer导出实体类和NHibernate xml文件 今天研究了一下通过PowerDesigner生成实体类和NHibernate所需要的xml文件,方法是通过Power Desi ...

  6. 导出Excel/Pdf/txt/json/XML/PNG/CSV/SQL/MS-Word/ Ms-Powerpoint/等通过tableExport.js插件来实现

    首先去我的云盘下载需要的js: 链接:https://pan.baidu.com/s/13vC-u92ulpx3RbljsuadWw 提取码:mo8m 页面代码: <!DOCTYPE html& ...

  7. 项目中运行报错: Loading XML bean definitions from class path resource [applicationContext.xml]

    记录一下: org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.s ...

  8. maven -- 学习笔记(二)之setting.xml配置说明(备忘)

    setting.xml配置说明,learn from:http://pengqb.javaeye.com,http://blog.csdn.net/mypop/article/details/6146 ...

  9. 只要项目是maven构建的,pom.xml中依赖的jar包全都默认去你电脑本地仓库去找

    只要项目是maven构建的,pom.xml中依赖的jar包全都默认去你电脑本地仓库去找

  10. 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置

     项目结构(所需jar包,配置文件) sqlMapConfig.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8&qu ...

随机推荐

  1. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  2. P2135 方块消除

    题目描述 Jimmy最近迷上了一款叫做方块消除的游戏.游戏规则如下:n个带颜色方格排成一列,相同颜色的方块连成一个区域(如果两个相邻方块颜色相同,则这两个方块属于同一区域).为简化题目,将连起来的同一 ...

  3. 用jQuery制作仿网易云课堂导航菜单效果

    最近做项目,用到类似的效果. 效果图如下: 直接上代码: HTML: <!DOCTYPE html> <html lang="en"> <head&g ...

  4. [SDOI2015][bzoj3990] 序列 [搜索]

    题面 传送门 思路 首先,这道题目有一个非常显然(但是我不会严格证明,只能意会一下)的结论:一个合法的操作序列中,任意两个操作是可以互换的 那么,这个结论加上本题极小的数据范围,为什么不搜索一下呢? ...

  5. POJ 2115 C-Looooops | exgcd

    题目 给出一个循环for(int i=A;i!=B;i+=C) 在mod (1<<k) 下是否可以退出循环 是,输出时间,否输出FORVEER 题解: 题意可以变换成 A+Cx=B (mo ...

  6. sass mixin 持续更新

    控制多行显示省略号... //文字溢出省略号@mixin coveText($num:1){ @if $num == 1{ white-space: normal; overflow: hidden; ...

  7. svn installation

    # yum install mod_dav_svn.x86_64 subversion-svn2cl.noarch=========================================== ...

  8. 汕头市队赛 SRM 07 C 整洁的麻将桌

    C 整洁的麻将桌 SRM 07 背景&&描述 天才麻将少女KPM立志要在日麻界闯出一番名堂.     KPM上周双打了n场麻将,但她这次没控分,而且因为是全民参与的麻将大赛,所以她的名 ...

  9. SQL Server 中使用 Try Catch 处理异常

    CREATE TABLE ErrorLog( errNum INT, ErrSev ), ErrState INT, ErrProc ), ErrLine INT, ErrMsg ) ) CREATE ...

  10. Linux查看进程堆栈信息命令

    jps -lvm #查看进程IDjstack -l <进程ID> # 查看进程堆栈信息