. Read multiple XML files in BIRT Report
You need to read multiple XML files in BIRT. You can’t use inbuilt XML Data Source Capability. Example: Input from the path
Q1_2015_Salesperson_1.xml
Q2_2015_Salesperson_1.xml
Q3_2015_Salesperson_1.xml
Q4_2015_Salesperson_1.xml
Q1_2015_Salesperson_2.xml
Q2_2015_Salesperson_2.xml
Q3_2015_Salesperson_2.xml
Q4_2015_Salesperson_2.xml
so on ....
Each XML might have the same tags or structure.
Solution:
1). You can use a Scripted Data Source and use java to parse the XML. If performance is not a concern, you can do something like,
import java.io.File;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class ReadFiles {
public static void main(String[] args) {
File dir = new File("D:/Work/sample"); //Directory where your file exists
File [] files = dir.listFiles();
for(File file : files) {
if(file.isFile() && file.getName().endsWith(".xml")) { //You can validate file name with extension if needed
ProcessFile(file, Entities); // Assumed you have declared Entities, may be list of other collection
}
}
System.out.println(Entities);
}
private static void ProcessFile(File fXmlFile, List<E> Entities) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("sindex");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("");
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Name : " + eElement.getElementsByTagName("name").item(0).getTextContent());
System.out.println("Count : " + eElement.getElementsByTagName("count").item(0).getTextContent());
Entity CE = new Entity(eElement.getElementsByTagName("name").item(0).getTextContent(),Integer.parseInt(eElement.getElementsByTagName("count").item(0).getTextContent()));
Entities.add(CE);
System.out.println("Entity added! ");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2). You can use esProc as Data Source and use SPL to parse the XML.
Here is the SPL script.
A | |
1 | =directory@p("D:/Work/sample/*.xml") |
2 | =A1.(xml(file(~).read())) |
3 | =A2.conj(~.array()) |
4 | return A3 |
A1:
A2:
A3:
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.
The sample data can be downloaded sample.zip. If you have any questions or comments, please leave them below.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL