项目笔记:导出XML和导出全部XML功能
Posted 古兰精
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目笔记:导出XML和导出全部XML功能相关的知识,希望对你有一定的参考价值。
前台代码:
//导出一条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 = "1";
createXml();
}
//导出xml功能
public void createXml(){
String ESswitch = getSession().getAttribute("ES").toString();
if("1".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("1")) {
if(OrganizationUtil.isNullMgrOrg(mgrOrgMap)){
dataList = new ArrayList<WindowsSafetyBaseLineLog>();
}else{
queryBean.setSession(getSession());
page = windowsSafetyBaseLineLogService.query(queryBean, 1, 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 = 0; 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()>0){
for (int i = 0; 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 = 0; 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() > 0){
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 = 0; i < sortArr.length; i++) {
queryBean.addOrderProperty("this." + sortArr[i],
"asc".equals(orderArr[i]) ? Order.ASC : Order.DESC);
}
}
}
导出XML效果:
以上是关于项目笔记:导出XML和导出全部XML功能的主要内容,如果未能解决你的问题,请参考以下文章