Multiple nested mail merge regions from XML data source

Hi,

I was checking out the examples for XML mail merge, however the example only covers simple merging from XML, would it be possible for you to provide some practical example of mail merge with regions, from an XML data source, possibly with some nested regions too?

I think it would make a great addition to your already good documentation!

Hi Matteo,

Thanks for your inquiry. I suggest you please read the article about Mail Merge from XML using IMailMergeDataSource. You can perform mail merge with regions using the code example shared in article.
https://docs.aspose.com/words/java/mail-merge-with-xml-data-source/

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

Hi,

I have already read that article, but it didn’t help much, because of two main reasons:

  1. the example is only about a very simple mail merge, without regions, while I am interested in a more complex use case

  2. the example itself seems wrong to me: the example XML you show has multiple “customer” record, but the template uses only the first!

wouldn’t you be able to provide a more complete working example, with region mail merge and nested region?

Hi Matteo,

Thanks for your inquiry. The shared code example works with mail merge with regions and nested regions. Please use XmlMailMergeDataSet instead of XmlMailMergeDataTable. I have attached a sample template document, XML data source and output document with this post for your kind reference.

Please let us know if you have any more queries.

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// Parse the XML data.
org.w3c.dom.Document xmlData = db.parse(MyDir + "Data.xml");
// Open a template document.
Document doc = new Document(MyDir + "Invoice Template.doc");
// Note that this class also works with a single repeatable region (and any nested regions).
// To merge multiple regions at the same time from a single XML data source, use the XmlMailMergeDataSet class.
doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));
// doc.getMailMerge().execute(new XmlMailMergeDataTable(xmlData, "customer"));
// Save the output document.
doc.save(MyDir + "Out.docx");

That’s exactly what I was looking for, thank you!

If I may say, it could be a nice addition to the official docs too.

I’d have one last question though: the sample XML you attached has the structure

<orders>
  <order>
    ...
    <item>
      ...
    </item>
    <item>
      ...
    </item></font>
  </order>
  <order>
    ...
  </order>
</orders>

but what if I would make it look like

<orders>
  <order>
    ...
    <items>
      <item>
        ...
      </item>
      <item>
        ...
      </item>
    </items>
  </order>
  <order>
    ...
  </order>
</orders>

then, how would I need to setup the template?

Hi Matteo,

Thanks for your inquiry.

*mtassinari:

If I may say, it could be a nice addition to the official docs too.*

The mail merge with region detail is mentioned in documentation. Please check the following commented lines of code in this documentation link.

// Note that this class also works with a single repeatable region (and any nested regions).
// To merge multiple regions at the same time from a single XML data source, use the XmlMailMergeDataSet class.
// doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));

*mtassinari:

but what if I would make it look like

… … … …

then, how would I need to setup the template?*

If the data source contains nested data getChildDataSource method will be called to retrieve the data for the child table. In the XML data source nested data this should look like this:

ParentName

Content

Hi,

where can I find the full code for XmlMailMergeDataSet and XmlMailMergeDataTable?

The article you linked in your previous answer seems to point to your github repository, but I cannot find those Java classes in it, only the usage example.

Hi Matteo,

Thanks for your inquiry. Please get the code of XmlMailMergeDataSet and XmlMailMergeDataTable classes from here:
https://github.com/aspose-words/Aspose.Words-for-Java