JAVA生成xml文件簡單示例
更新時間:2023年07月26日 14:08:14 作者:甜甜圈的小餅干
這篇文章主要介紹了JAVA生成xml文件的相關資料,在Java中可以使用DOM或者JDOM來生成XML文件,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
JAVA生成xml文件
一、導包
自動生成xml文件,使用到的jar包為dom4j
<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é)點,根據需求添加,這里我只是設置了一個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();
}
}
}三、結果展示
<?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>總結
到此這篇關于JAVA生成xml文件的文章就介紹到這了,更多相關JAVA生成xml文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
LinkedBlockingQueue鏈式阻塞隊列的使用和原理解析
這篇文章主要介紹了LinkedBlockingQueue鏈式阻塞隊列的使用和原理解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

