博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将java保存成.xml文件
阅读量:4319 次
发布时间:2019-06-06

本文共 4725 字,大约阅读时间需要 15 分钟。

       因为我做的是摄像头的项目,所以在项目中需要用的这一部分的知识点,做的时候很生疏,在网上看了一些教程才做出来的,所以想自己把它记录下来,以后可以经常看看。

       首先在开发软件上自己新建一个java项目,在里面新建一个文件,此处省略,直接介绍步骤:
1、在文件下面,声明静态变量,用来存放生成的.xml文件路径;

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

2、新建一个方法,我这里用到的是摄像头设备的IP和参数变量,所以要将他们当做参数传递给这个方法,里面用的两个对象,现在这里介绍一下,分别是Element和Document。

    1)Element 对象表示 XML 文档中的元素。元素可包含属性、其他元素或文本。如果元素含有文本,则在文本节点中表示该文本。

/**      * 这里的class、id就是Element对象,可以存在多个Element对象,就像示例中所说的嵌套的形式了      */ 

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

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

你可能感兴趣的文章
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
学习进度
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>