Mail Merge using XML data source with region

We have been trying a mail merge reading data from an XML file. The word template contains a region that needs to be repeated. How should we define the region in the java code while using IMailMergeDataSource.

The XML payload has been attached herewith.

Hi Mayur,


Thanks for your inquiry.

Using the XmlMailMerge example project you can perform repeating mail merge using the same code. Simply load the XML data into the XmlMailMergeDataSet class.

Each node in your XML file with child nodes is represented as its own XmlMailMergeDataTable. The XmlMailMergeDataSet class represents all of these tables in one group and it can be passed to the executeWithRegions mail merge method to merge all data including repeating or hierarchical data.

I have attached a template which can be merged using the code below and your data source that you attached. Please let us know if there are any problems.

// Parse the XML data.
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

org.w3c.dom.Document xmlData = db.parse("LocalTaxComplaint.xml");

// Open a template document.
Document doc = new Document("MailMegeTemplate.docx");

// Remove any empty paragraphs that appear during mail merge.
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);

// Merge the data
doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));

There are some useful things to note:
  • Due to the nature of XML, you must always have a "root" node, which in your data file at the moment is the LocalTaxCompliant node. If you are expecting to have more than one LocalTaxCompliant, in order for it to be repeated you would have add another node as the root.
  • The extra wrapper node Properties is not required (however there is no harm in it being there of course). It just means an extra "TableStart\TableEnd" in the template. If you removed the "Properties" node and corresponding TableStart\TableEnd in the template then everything will merge correctly as the multiple "Property" nodes becomes a data table on its own.
If we can help with anything else, please feel free to ask.

Thanks,

Hi Mayur,

Thanks for your query. Please read following documentation link for your kind reference.

http://www.aspose.com/docs/display/wordsjava/How+to++Mail+Merge+from+XML+using+IMailMergeDataSource

http://www.aspose.com/docs/display/wordsjava/How+to++Execute+Mail+Merge+with+Regions

http://www.aspose.com/docs/display/wordsjava/How+to++Execute+Mail+Merge+with+Regions

http://www.aspose.com/docs/display/wordsjava/How+to++Use+Nested+Mail+Merge+Regions

Hope this helps you. Please let us know if you have any more queries.

Adam,

XmlMailMergeDataSet - Is this part of Aspose package or a user defined class? Btw, I have been using XmlMailMergeDataTable that implements IMailMergeDataSource. If this is custom class, please provide me a sample.

It worked. Thanks Adam for your tips!

Hi Mayur,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi Adam,


could please attach the LocalTaxComplaint.xml file please ?

Regards,
Sovanna

Hi Sovanna,


Thanks for your inquiry. Please find the LocalTaxComplaint.xml file in attachment. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.