Use XML as Datasource in BIRT Report

You have to use an XML as a data source in your report. The XML has multiple child tags with the same name(ex. author). Refer to 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>

1) Using XML Datasource

You add an XML data source and a dataset in your report. You make the following mappings in my XML dataset.

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

In the data set, you are getting three records, one for each of the tags. 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 recognized by the BIRT. You need to get both “Roy” & “Jon” from the second Book element. And, you need to get both “Harry” & “Potter” from the third Book element. You can’t get all the values from tags with the same name into the dataset. You would rename the author tag to authorFirstName and authorLastName so that there is no confusion on Birt’s end.

2) Using esProc Datasource

Here is the esProc SPL script:


A
1 =file("./BookStore.xml")
2 =xml(A1.read())
3 =A2.BookStore.Book
4 =A3.new(title:title,   if(ifa(author),author.concat("&"),author):author,
      if(ifstring(copies),int(replace(copies,";","")),copies):copies,   price)


A3:

imagepng

A4: Cleans up and unifies column data types and returns the final result.

imagepng

The report can be designed in the same way as you would if you were retrieving the data from a database. For detail SPL integration with BIRT, see How to Call an SPL Script in BIRT.

For more examples, procedure XML files refer to the following XML data parsing and calculation.

If you have any questions or comments please leave them below.