Tags in XML files are not getting merged when using mailmerge android via java(Aspose.words)


#1

I am using the mailmerge snippet provided in the following link https://docs.aspose.com/display/wordsjava/How+to+Mail+Merge+from+XML+using+IMailMergeDataSource to merge the data in rtf template from the xml file which is stored locally in android mobile internal storage.
I am attaching the source code, data file - xml file,rtf template and the expected pdf output document. I am using Android Studio IDE.

This is my sample code,

Document doc = new Document(filePath); // filePath would return a value as (/storage/emulated/0/TEMPLATE.rtf). We are retrieving the template file from the mobile device storage.

javax.xml.parsers.DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

org.w3c.dom.Document xmlData = db.parse(new File(xmlPath)); //xmlPath would return a value as (/storage/emulated/0/TEMPLATE.xml). We are retrieving the template file from the mobile device storage.

doc.getMailMerge().setPreserveUnusedTags(true);

// 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.
// e.g doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));

doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));

doc.save(outputPath); //(e.g., /storage/emulated/0/TEMPLATE.pdf) 

I am not getting any runtime error, however data from the xml file is not merged with rtf template.Template.zip (164.7 KB)


#2

@Mohan_Narasimman,

Please also create a simplified standalone runnable Android Application (source code without compilation errors) that helps us to reproduce your current problem on our end, ZIP and attach it here for testing. You can also upload the ZIP file to Dropbox and share the Download link here for testing. Thanks for your cooperation.


#3

@awais.hafeez

I have created the runnable Android Application and attached the dropbox link for your review. The project is written in Android Studio IDE.

Android project dropbox link : https://www.dropbox.com/sh/6mfgsxcdkowcni5/AAAdymQTLe8g9MlyE-GCch-ua?dl=0&lst=

Input file and Expected output file dropbox link : https://www.dropbox.com/sh/cm01bly6z1gz53n/AADovr8icz4MS21hOaOA66pJa?dl=0

I am waiting for your response.


#4

@Mohan_Narasimman,

The problem occurs because there are no real “merge fields” in your template document. That document only contains FORMTEXT fields. You can replace those FORMTEXT fields with MERGEFIELDs by using the following code:

...
...
String filePath = "E:\\temp\\template\\Template" + ".rtf";
String xmlPath = "E:\\temp\\template\\Template" + ".xml";
String outputPath = "E:\\temp\\template\\aw-19.6" + ".docx";

// filePath would return a value as (/storage/emulated/0/TEMPLATE.rtf). We are retrieving the template file from the mobile device storage.
Document doc = null;
try {
    doc = new Document(filePath);
    DocumentBuilder builder = new DocumentBuilder(doc);

    for(Field field : (Iterable<Field>) doc.getRange().getFields())
    {
        if (field.getType() == FieldType.FIELD_FORM_TEXT_INPUT)
        {
            FieldFormText formText = (FieldFormText)field;
            builder.moveTo(formText.getStart());
            builder.insertField("MERGEFIELD \"" + formText.getResult() + "\"", null);
            field.remove();
        }
    }

    javax.xml.parsers.DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
...
...

After that you need to make sure that mail merge regions (TableStart and TableEnd merge fields) are well formed. After when you fix the template, the code should work. Please check the following article:
Mail Merge with Regions Explained

Hope, this helps.


#5

@awais.hafeez,

Can you please explain which part of my template needs to be modified and how to fix those part of the template. It will be helpful to fix the problem.

I am waiting for your response.


#6

@Mohan_Narasimman,

As shown in following screenshot, you need to replace those FORMTEXT fields with MERGEFIELDs. Please see my previous post for details.


#7

@awais.hafeez,

The file given in example template (TestFile.doc) and xml file (Customer.xml) is not merging. Can you please send the sample template and xml file.

I am waiting for your reference.


#8

@Mohan_Narasimman,

I have transformed FORMTEXT fields in your template document to actual MERGEFIELDs by using the following code:

String filePath = "E:\\temp\\template\\Template" + ".rtf";
String xmlPath = "E:\\temp\\template\\Template" + ".xml";
String outputPath = "E:\\temp\\template\\aw-19.7" + ".docx";

Document doc = new Document(filePath);
DocumentBuilder builder = new DocumentBuilder(doc);

for (Field field : (Iterable<Field>) doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_FORM_TEXT_INPUT) {
        FieldFormText formText = (FieldFormText) field;
        builder.moveTo(formText.getStart());
        builder.insertField("MERGEFIELD \"" + formText.getResult() + "\"", null);
        field.remove();
    }
}

doc.save("E:\\temp\\template\\awjava-19.7.docx");

See attachment: awjava-19.7.zip (33.8 KB)

Next, you need to further rework this template on your end and make sure that mail merge regions (TableStart and TableEnd merge fields) are well formed. After when you fix/rework the template on your end, the code should work. Please check the following article:
Mail Merge with Regions Explained