在 Aspose.Words for Java 中使用 XML 數據

在 Aspose.Words for Java 中使用 XML 資料簡介

在本指南中,我們將探討如何使用 Aspose.Words for Java 處理 XML 資料。您將學習如何執行郵件合併操作,包括巢狀郵件合併,以及如何將 Mustache 語法與資料集結合使用。我們將提供逐步說明和原始程式碼範例來幫助您入門。

先決條件

在我們開始之前,請確保您具備以下先決條件:

  • Aspose.Words for Java安裝。
  • 客戶、訂單和供應商的範例 XML 資料檔。
  • 郵件合併目標的範例 Word 文件。

郵件與 XML 資料合併

1. 基本郵件合併

若要使用 XML 資料執行基本郵件合併,請依照下列步驟操作:

DataSet customersDs = new DataSet();
customersDs.readXml("Your Directory Path" + "Mail merge data - Customers.xml");
Document doc = new Document("Your Directory Path" + "Mail merge destinations - Registration complete.docx");
doc.getMailMerge().execute(customersDs.getTables().get("Customer"));
doc.save("Your Directory Path" + "BasicMailMerge.docx");

2. 巢狀郵件合併

對於巢狀郵件合併,請使用以下程式碼:

DataSet pizzaDs = new DataSet();
pizzaDs.readXml("Your Directory Path" + "Mail merge data - Orders.xml");
Document doc = new Document("Your Directory Path" + "Mail merge destinations - Invoice.docx");
doc.getMailMerge().setTrimWhitespaces(false);
doc.getMailMerge().executeWithRegions(pizzaDs);
doc.save("Your Directory Path" + "NestedMailMerge.docx");

使用資料集的 Mustache 語法

若要將 Mustache 語法與資料集結合使用,請執行下列步驟:

DataSet ds = new DataSet();
ds.readXml("Your Directory Path" + "Mail merge data - Vendors.xml");
Document doc = new Document("Your Directory Path" + "Mail merge destinations - Vendor.docx");
doc.getMailMerge().setUseNonMergeFields(true);
doc.getMailMerge().executeWithRegions(ds);
doc.save("Your Directory Path" + "MustacheSyntaxUsingDataSet.docx");

結論

在本綜合指南中,我們探討如何透過 Aspose.Words for Java 有效使用 XML 資料。您已經了解如何執行各種郵件合併操作,包括基本郵件合併、巢狀郵件合併以及如何將 Mustache 語法與資料集結合使用。這些技術使您能夠輕鬆自動產生文件並進行自訂。

常見問題解答

如何準備用於郵件合併的 XML 資料?

確保您的 XML 資料遵循所需的結構,並定義了表格和關係,如提供的範例所示。

我可以自訂郵件合併值的修剪行為嗎?

是的,您可以控制在郵件合併期間是否修剪前導和尾隨空格,方法是使用doc.getMailMerge().setTrimWhitespaces(false).

Mustache 語法是什麼?什麼時候該使用它?

Mustache 語法可讓您以更靈活的方式設定郵件合併欄位的格式。使用doc.getMailMerge().setUseNonMergeFields(true)啟用 Mustache 語法。