JAVA生成xml文件
一、導包
自動生成xml文件,使用到的jar包為dom4j文章來源地址http://www.zghlxwxcb.cn/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.創(chuàng)建dom對象
Document document = DocumentHelper.createDocument();
// 3.添加節(jié)點,根據(jù)需求添加,這里我只是設置了一個head節(jié)點,下面有name和age兩個子節(jié)點
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();
}
}
}
三、結(jié)果展示
<?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>
文章來源:http://www.zghlxwxcb.cn/news/detail-546292.html
到了這里,關(guān)于JAVA生成xml文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!