Complex XML merge doesn't work

Hi everybody,

I was recently tinkering with the XML mail merge feature, but I am having trouble making it work on a rather big XML input string which I tried building from many SQL queries.

Attached to the post you’ll find three files:

  • template.doc, which is the template used for mail merging
  • data.xml, which is a copy of the extracted data
  • result.doc, which is the resulting file after calling the merge function
  • wanted.doc, which is the result I’d like to obtain, as it is currently proceduced by a different service

Merging is done like this:

// first setup cleanup function
doc.getMailMerge().setCleanupOptions(
// rimozione regioni non compilate
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
// rimozione campi non compilati
| MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
// rimozione campi innestati
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
);

// then merge all XML in one pass
doc.getMailMerge().executeWithRegions(new com.agews.suite626.mailmerge.xml.DataSet(xmlString));

where com.agews.suite626.mailmerge.xml.DataSet is just a copy/paste of your XmlMailMergeDataTable from GitHub.

Am I doing anything wrong here, or misusing the XML merge class?

The main problems I am noticing here are:

  • fields which are not inside a region are not merged
  • non-merged fields are not removed even though specified in cleanup options

Hi Matteo,

Thanks for your inquiry. In your case, we suggest you please use DataSet.readXml as shown in following code example instead of XmlMailMergeDataTable.

You can pass DataSet to MailMerge.executeWithRegions method to achieve your requirements. You just need to add TableStart:root at the start of document and TableEnd:root at the end of document. The root is TableName from your xml.

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

Document doc = new Document(MyDir + "template.doc");
DataSet ds = new DataSet();
ds.readXml(MyDir + "data.xml");
doc.getMailMerge().executeWithRegions(ds);
doc.save(MyDir + "Out.docx");

I tried first to only wrap the whole document between TableStart:root and TableEnd:root, still using the class, and I ended up with an empty document, where it appears no mail merge was performed at all, and so because of cleanup options, all regions were removed.

Sadly using DataSet.readXml is currently not an option, since it does not seem to be available in the version of Aspose.Words for Java we are using, and we cannot update right now.

Any workaround you can suggest? Should we just split the merge in two steps, first with execute() and then executeWithRegions()?

Hi Matteo,

Thanks for your inquiry. Please check the attached modified template document and Data.xml. We have highlighted the TableStart and TableEnd fields in attached document.

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

javax.xml.parsers.DocumentBuilder db =
javax.xml.parsers.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 + "template.doc");
doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));

Hi,

thanks for the suggestion, I’ll try that asap!