Free Support Forum - aspose.com

Iterate xml and create word document

Hi,

We have an xml as shown below. We have to iterate through the xml and insert the data into a table in word.

xml File:

<Results>
	<Row>
         <value1>xyz</value1>
         <value2>xss</value2>
        </Row>
<Row>
         <value1>dfa</value1>
         <value2>xsdfas</value2>
        </Row>
<Row>
         <value1>asb</value1>
         <value2>dse</value2>
        </Row>
<Row>
         <value1>dfadf</value1>
         <value2>fadsf</value2>
        </Row>
</Results>

Currently we have given the below code in the word template: (value1 and value 2 are in different columns of a table).
<<foreach [ result in Row]>> | <<[result.value1]>> | <[ result.value2]>> <</foreach>>

The java code for pdf generation is given below,

    Document doc = new Document(dataDir + fileName);
        ReportingEngine engine = new ReportingEngine();
        PdfSaveOptions options = new PdfSaveOptions();
        options.getOutlineOptions().setDefaultBookmarksOutlineLevel(1);
        options.getOutlineOptions().setDefaultBookmarksOutlineLevel(2);
        DataSet ds = new DataSet();
        ds.readXml(dataDir+"AcutalData4.xml");
        engine.buildReport(doc, ds);
        dataDir = dataDir + "AcutalData_otput4.pdf";
        doc.save(dataDir);

But After a certain limit it is throwing IndexOutofBound Exception.

Can you please help me on this?

Hi Nithin,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for Java 17.6 and have not found the shared issue. Please use Aspose.Words for Java 17.6 and get the documents from here:

If you still face problem, please share your input document and XML here for testing. We will investigate the issue and provide you more information on this.

The issue still exists. PFA files.sampledoc.zip (10.3 KB)

Hi Nithin,

Thanks for sharing the document and XML. We have tested the scenario using latest version of Aspose.Words for Java 17.6 and have not found the shared issue. Please use Aspose.Words for Java 17.6. In case you are using Aspose.Words in evaluation mode, we suggest you please request for 30-days temporary license from here:
Get temporary license

Please read following article about applying license.
Applying a License

Hi Tahir,

Did you use the same java code I’ve given. Can you please share the code you have used.

Thanks and Regards,
Nithin George

Hi Nithin,

Thanks for your inquiry. Yes, I used the same code example. Please get the output PDF document from here:

Document doc = new Document(MyDir + "sampledoc.docx");
ReportingEngine engine = new ReportingEngine();
PdfSaveOptions options = new PdfSaveOptions();
options.getOutlineOptions().setDefaultBookmarksOutlineLevel(1);
options.getOutlineOptions().setDefaultBookmarksOutlineLevel(2);
DataSet ds = new DataSet();
ds.readXml(MyDir+"xmlData.xml");
engine.buildReport(doc, ds);

doc.save(MyDir + "Without PdfSaveOptions 17.6.pdf");
doc.save(MyDir + "17.6.pdf", options);

Best Regards,
Tahir Manzoor