JAVA生成xml文件
一、导包
自动生成xml文件,使用到的jar包为dom4j文章来源地址https://www.toymoban.com/news/detail-546292.html
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
二、书写工具包
package com.rainfe.tdm.df.util;/**
* @author by XXX
* @date 2022/11/21.
* <p>
* 描述:
*/
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.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
TDM
*
XmlUtil
* @author : fzt
* @date : 2022-11-21 14:29
**/
public class XmlUtil {
public static void main(String[] args) {
// 1.声明文件名称
String fileName = "xml_test";
// 2.创建dom对象
Document document = DocumentHelper.createDocument();
// 3.添加节点,根据需求添加,这里我只是设置了一个head节点,下面有name和age两个子节点
Element esbEnvelop = document.addElement("ESBEnvelop");
Element esbHead = esbEnvelop.addElement("ESBHead");
Element esbBody = esbEnvelop.addElement("ESBBody");
Element appRequest = esbBody.addElement("AppRequest");
Element appReqHead = appRequest.addElement("AppReqHead");
Element tradeCode = appReqHead.addElement("TradeCode");
Element reqSerialNo = appReqHead.addElement("ReqSerialNo");
Element tradeTime = appReqHead.addElement("TradeTime");
Element tradeDescription = appReqHead.addElement("TradeDescription");
Element tradeLogLevel = appReqHead.addElement("TradeLogLevel");
Element reserved = appReqHead.addElement("Reserved");
Element appReqBody = appRequest.addElement("AppReqBody");
Element table = appReqBody.addElement("table").addAttribute("name", "表1").addAttribute("id", "Bom-01-01-eee");
Element rows = table.addElement("rows");
rows.addElement("row").addAttribute("key","value").addAttribute("key1","value1");
tradeCode.addText("这是tradeCode");
reqSerialNo.addText("这是reqSerialNo");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format1 = sdf.format(date);
tradeTime.addText(format1);
tradeLogLevel.addText("1");
// 4、格式化模板
//OutputFormat format = OutputFormat.createCompactFormat();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
// 5、生成xml文件
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
writer.close();
} catch (IOException e) {
System.out.println("生成xml文件失败。文件名【" + fileName + "】");
}
// 6、生成的XML文件
// 7、利用文件输出流输出到文件, 文件输出到了您的项目根目录下了
try (FileOutputStream fos = new FileOutputStream(fileName + ".xml")) {
fos.write(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、结果展示
<?xml version="1.0" encoding="UTF-8"?>
<ESBEnvelop>
<ESBHead/>
<ESBBody>
<AppRequest>
<AppReqHead>
<TradeCode>这是tradeCode</TradeCode>
<ReqSerialNo>这是reqSerialNo</ReqSerialNo>
<TradeTime>2022-11-21 15:02:27</TradeTime>
<TradeDescription/>
<TradeLogLevel>1</TradeLogLevel>
<Reserved/>
</AppReqHead>
<AppReqBody>
<table name="表1" id="Bom-01-01-eee">
<rows>
<row key="value" key1="value1"/>
</rows>
</table>
</AppReqBody>
</AppRequest>
</ESBBody>
</ESBEnvelop>
文章来源:https://www.toymoban.com/news/detail-546292.html
到了这里,关于JAVA生成xml文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!