JAVA生成xml文件簡單示例
更新時間:2023年07月26日 14:08:14 作者:甜甜圈的小餅干
這篇文章主要介紹了JAVA生成xml文件的相關(guān)資料,在Java中可以使用DOM或者JDOM來生成XML文件,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
JAVA生成xml文件
一、導(dǎo)包
自動生成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é)點,根據(jù)需求添加,這里我只是設(shè)置了一個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>總結(jié)
到此這篇關(guān)于JAVA生成xml文件的文章就介紹到這了,更多相關(guān)JAVA生成xml文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot連接不同數(shù)據(jù)庫的寫法詳解
這篇文章主要介紹了springboot連接不同數(shù)據(jù)庫的寫法?,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
2023-04-04
Java實戰(zhàn)玩具商城的前臺與后臺實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+JSP+SSM+Springboot+Jsp+maven+Mysql實現(xiàn)一個玩具商城系統(tǒng),大家可以在過程中查缺補漏,提升水平
2022-01-01
將java程序打包成可執(zhí)行文件的實現(xiàn)方式
本文介紹了將Java程序打包成可執(zhí)行文件的三種方法:手動打包(將編譯后的代碼及JRE運行環(huán)境一起打包),使用第三方打包工具(如Launch4j)和JDK自帶工具(jpackage),每種方法都有其優(yōu)缺點,可根據(jù)實際需求選擇合適的方式
2025-02-02 
