将java保存成.xml文件

       因为我做的是摄像头的项目,所以在项目中需要用的这一部分的知识点,做的时候很生疏,在网上看了一些教程才做出来的,所以想自己把它记录下来,以后可以经常看看。
       首先在开发软件上自己新建一个java项目,在里面新建一个文件,此处省略,直接介绍步骤:
1、在文件下面,声明静态变量,用来存放生成的.xml文件路径;

    /**
     * 存放接口的路径
     */
    private static String currentFolder="./config/HKVideoDate/";

2、新建一个方法,我这里用到的是摄像头设备的IP和参数变量,所以要将他们当做参数传递给这个方法,里面用的两个对象,现在这里介绍一下,分别是Element和Document。
    1)Element 对象表示 XML 文档中的元素。元素可包含属性、其他元素或文本。如果元素含有文本,则在文本节点中表示该文本。

     /**
      * 这里的class、id就是Element对象,可以存在多个Element对象,就像示例中所说的嵌套的形式了
      */
 <class name="VideoConfig" table="videoinfo">  
      <id name="ip" column="ip" type="java.lang.String">  
          <generator class="assigned"/>  
      </id> 
 </class>

在代码中创建根节点的方式

Element root = new Element("video");

    2)Document对象是一棵文档树的根,可为我们提供对文档数据的最初(或最顶层)的访问入口,所以在代码中只有一个就可以了。
3、然后将获取到的摄像头的视频参数放入到Element对象中;

Element root = new Element("video");
Document doc = new Document(root);
if(video!=null){
    root.setAttribute("系统名称",video.getSysName());
    root.setAttribute("设备状态", video.getStatus());
    ……
    }

4、输出xml文件到本地

XMLOutputter XMLOut = new XMLOutputter();
File file = new File(ipPath);
if(!file.exists()){
    file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path);
XMLOut.output(doc, fos);
fos.close();

可能会抛出异常,捕获就好了。
以上就是将java文件保存到.xml文件的全过程,下面我将完整代码附上,希望可以帮助到大家。

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;
    }
}
原文地址:https://www.cnblogs.com/G1432291874/p/9346027.html