Hi,
I am facing issue in reading the XML element in the merge filed when the xml element is in the root level. when the element is in the table level i am merger the filed sucessfully.
<?xml version="1.0" encoding="UTF-8" ?>
<sample>
<companyname>ABC PVT ltd.</companyname>
<salary>Manik Street</salary>
<row>
<name>Raja</name>
<address>c3-270</address>
<street>Manik Street</street>
<state>Tamil Nadu</state>
<country>India</country>
</row>
</sample>
In the above example i am able to read the element (Name,Address,Street,Country) through the root element “row”,but when trying to read the XML element (company name,salary ) in merger filed we are not getting.
attaching the sample code,template ,XML and generated aspose PDF in ZIP format.
Any solution would be help full for as.
Thanks & Regards,Dinesh
Hi Dinesh,
Thanks for your inquiry. We suggest you please use DataSet.readXml to load the xml into DataSet and use following code example. We have modified the xml and attached it with this post.
DataSet ds = new DataSet();
ds.readXml(dataDir + "sample_Mapping.xml");
doc.getMailMerge().executeWithRegions(ds);
//pass the first row of table to MailMerge.execute method.
doc.getMailMerge().execute(ds.getTables().get("sample").getRows().get(0));
You may also use XmlMailMergeDataSet and XmlMailMergeDataTable classes to achieve your requirements. Please use following code snippet.
doc.getMailMerge().execute(new XmlMailMergeDataTable(xmlData, "sample"));
doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));
Hi ,
Is there any solution to generate the template without changing the sample XML.
<?xml version="1.0" encoding="UTF-8" ?>
<sample>
<companyname>ABC PVT ltd.</companyname>
<salary>Manik Street</salary>
<row>
<name>Raja</name>
<address>c3-270</address>
<street>Manik Street</street>
<state>Tamil Nadu</state>
<country>India</country>
</row>
</sample>
Thanks & Regards, Dinesh
Hi Dinesh,
Thanks for your inquiry. You are using simple mail merge and mail merge with regions. Please use following code snippet to merge data from shared xml. Hope this helps you.
DataSet ds = new DataSet();
ds.readXml(dataDir + "Test001.xml");
doc.getMailMerge().executeWithRegions(ds);
//pass the first row of table to MailMerge.execute method.
doc.getMailMerge().execute(ds.getTables().get(**"sample"**).getRows().get(0));