简体   繁体   中英

Issues with XML Data source

I have to use an XML as a data source in my report. My xml has multiple child tags with the same name(ex. author). Please refer the XML pasted below.

<BookStore>
<Book>
<title>History</title>
<author>Tom</author>
<copies>10;</copies>
<price>80</price>
</Book>

<Book>
<title>Basic Mathematics</title>
<author>Roy</author>
<author>Jon</author>
<copies>5</copies>
<price>100</price>
</Book>

<Book>
<title>Java</title>
<author>Harry</author>
<author>Potter</author>
<copies>6</copies>
<price>100</price>
</Book>
</BookStore>

I have added a XML data source and a dataset in my report. I have made the following mappings in my XML dataset.

Row mapping: /BookStore/Book and Column Mapping: Mapped all the child tags

In the data set, I am getting three records, one for each of the tag. But, the author column contains the value of only the first tag.For example, the second record contains only "Roy". The second author tag is not being recognised by the BIRT. I need to get both "Roy" & "Jon" from the second Book element. And, I need to get both "Harry" & "Potter" from the third Book element. Can you please let me know how to get all the values from tags with the same name into the dataset.

Appreciate your help. Please let me know how to design the XML dataset.

In your data set you could make multiple columns such as author[1], author[2], author[3]. Then you could make a computed column that concatenates those columns together.

I do not think that XPath functions are supported in BIRT reports.

public class ExportPDF {

    public static void main(String[] args) throws JRException {
        String sourceFileName = "E:/ireport/issueVoucher.jasper";

        String printFileName = null;

        JRXmlDataSource beanColDataSource = new JRXmlDataSource("E:/ireport/dbsource.xml","/root/data");


        Map<String, Object> parameters = new HashMap<String,Object>();
        parameters.put("reqNo", "33434");
        parameters.put("requestorName", "Shaan");
        parameters.put("empCode", "E03030");
        parameters.put("voucherNo", "E03030");
        parameters.put("issueDate", "E03030");
        parameters.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, beanColDataSource);

        try {
            printFileName = JasperFillManager.fillReportToFile(sourceFileName, parameters, beanColDataSource);
            if (printFileName != null) {
                JasperExportManager.exportReportToPdfFile(printFileName, "E:/sample_report.pdf");

                JRXlsExporter exporter = new JRXlsExporter();
                exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, printFileName);
                exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "E:/sample_report.xls");

                exporter.exportReport();
                System.out.println("done!!!!!!!!!!!!!!!!");
            }
        }catch (JRException e) {
            e.printStackTrace();
        }
    }
}

您寻求帮助来了解如何设计XML数据集,我将author标记重命名为authorFirstNameauthorLastName以免在Birt的authorLastName造成混乱。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM