将java保存成.xml文件

时间:2025-04-03 07:34:02
package com.sun.video.hk.api.vo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.log4j.Logger; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.XMLOutputter; public class HKVideoUtil { /** * 存放接口的路径 */ private static String currentFolder="./config/HKVideoDate/"; /** * 将设备信息保存到XML文件 */ public static String saveHKVideoManager(String ip,HKVideo video){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String today=sdf.format(new Date()); String ipPath=currentFolder+today+"/"+ip+"/"; String path=ipPath+ip+"_"+System.currentTimeMillis()+".xml"; Element root = new Element("video"); Document doc = new Document(root); if(video!=null){ if(video.getSysName()!=null&&video.getSysName()!=""){ root.setAttribute("系统名称",video.getSysName()); } if(video.getChannelName()!=null&&video.getChannelName()!=""){ root.setAttribute("通道名称",video.getChannelName()); } if(video.getStatus()!=null&&video.getStatus()!=""){ root.setAttribute("设备状态", video.getStatus()); } if(video.getVideoStatus()!=null&&video.getVideoStatus()!=""){ root.setAttribute("视频状态", video.getVideoStatus()); } if(video.getSignStatus()!=null&&video.getSignStatus()!=""){ root.setAttribute("信号状态",video.getSignStatus()); } if(video.getPootNum()>=0){ root.setAttribute("通道数",String.valueOf(video.getPootNum())); } if(video.getSerial()!=null&&video.getSerial()!=""){ root.setAttribute("序列号",video.getSerial()); } if(video.getDiskNum()!=null&&video.getDiskNum()!=""){ root.setAttribute("硬盘数",video.getDiskNum()); } if(video.getMacAddress()!=null&&video.getMacAddress()!=""){ root.setAttribute("mac地址",video.getMacAddress()); } if(video.getSubnetMask()!=null&&video.getSubnetMask()!=""){ root.setAttribute("子网掩码",video.getSubnetMask()); } Element disk = new Element("disk"); Element diskEle = new Element("disk"); if(video.getDiskName()!=null&&video.getDiskName()!=""){ diskEle.setAttribute("硬盘名称",video.getDiskName()); } if(video.getCapacity()>=0){ diskEle.setAttribute("磁盘总容量",String.valueOf(video.getCapacity())); } if(video.getFreeSpace()>=0){ diskEle.setAttribute("剩余空间",String.valueOf(video.getFreeSpace())); } if(video.getDiskStatus()!=null&&video.getDiskStatus()!=""){ diskEle.setAttribute("硬盘状态",video.getDiskStatus()); } disk.addContent(diskEle); root.addContent(disk); } XMLOutputter XMLOut = new XMLOutputter(); File file = new File(ipPath); if(!file.exists()){ file.mkdirs(); } try { FileOutputStream fos = new FileOutputStream(path); XMLOut.output(doc, fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } return path; } }