话不多数,直接上代码:
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();
}
}
}
看一下结果: