如何用dom4j创建XML文件并写入节点

话不多数,直接上代码:

package WinForm.showReports;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * @author wangyan
 * @date 2019-03-28  下午 02:08
 */
public class ReportXML {
  
   public static void main(String[] args){
        saveXml();
    }

    public static void saveXml(){
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("Element"); //创建根元素
        Element schema=root.addElement("Schema");

        schema.addAttribute("ID","123");
        schema.addAttribute("Name","测试方案2019-03-28 12:90:23");

        Element prodef=schema.addElement("Prodef");

        prodef.addAttribute("ID","234");
        prodef.addAttribute("Name","首次登记");
        prodef.addAttribute("PackageName","国有");

        Element prodefchild=prodef.addElement("ProdefChild");
        prodefchild.addAttribute("ID","345");
        prodefchild.addAttribute("Name","单个权利");

        Element report=prodef.addElement("Report");
        report.addAttribute("ID","34141");
        report.addAttribute("Name","测试报告");


        Element reportchild=report.addElement("ReportChild");
        reportchild.addAttribute("ID","1");
        reportchild.addAttribute("Name","测试结果概览");
        reportchild.addAttribute("Text","测试模块:断言调试-国有-首次登记    测试通过:2\t\t测试失败:2\t\t测试跳过:0");
        reportchild.addAttribute("Path","D:\\work_space\\AutoTest_Reports\\2019-03-25\\09_15\\html\\index.html");


        Element screenshoot=prodef.addElement("ScreenShoot");
        screenshoot.addAttribute("ID","2");
        screenshoot.addAttribute("Name","截图展示");

        Element screenshootchild=screenshoot.addElement("ScreenShootChild");
        screenshootchild.addAttribute("ID","1");
        screenshootchild.addAttribute("Name","资料收取");
        screenshootchild.addAttribute("Path","D:\\work_space\\SootScreens\\2019-03-27\\20_02\\资料收取20_02.png");

        screenshootchild.addAttribute("ID","2");
        screenshootchild.addAttribute("Name","大保存");
        screenshootchild.addAttribute("Path","D:\\work_space\\SootScreens\\2019-03-26\\11_04\\大保存 11_04.png");


        Element frreport=prodef.addElement("FRreport");
        frreport.addAttribute("ID","3");
        frreport.addAttribute("Name","帆软表单");
        Element frreportchild=frreport.addElement("FRreportChild");
        frreportchild.addAttribute("ID","1");
        frreportchild.addAttribute("Name","使用权表2019-03-20 19");
        frreportchild.addAttribute("Path","D:\\work_space\\FR\\2019-03-20\\19_05\\使用权表2019-03-20 19.pdf");


        OutputFormat format = OutputFormat.createPrettyPrint();
             //设置xml文档的编码为utf-8
        format.setEncoding("utf-8");
        Writer out;
        try {
            //创建一个输出流对象
            out = new FileWriter("D:/ReportSchema.xml");
            //创建一个dom4j创建xml的对象
            XMLWriter writer = new XMLWriter(out, format);
            //调用write方法将doc文档写到指定路径
            writer.write(document);
            writer.close();
            System.out.print("生成XML文件成功");
        } catch (IOException e) {
            System.out.print("生成XML文件失败");
            e.printStackTrace();
        }

    }
}

看一下结果:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值