. Parse an XML file in BIRT
You have a report that is using an XML file as the data source.
an XML file that looks like this:
<?xml version="1.0"?>
<library>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author name="Giada De Laurentiis" country="it"/>
<year>2005</year>
<info>asdfghjkl</info>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author name="J K. Rowling" country="uk" />
<year>2005</year>
<info>asdfghjkl</info>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author name="James McGovern" country="us" />
<author name="Per Bothner" country="us" />
<info>asdfghjkl</info>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author name="Erik T. Ray" country="us" />
<year>2003</year>
<info>asdfghjkl</info>
</book>
</library>
1). You can use a Scripted Data Source and use java to parse the XML.
In Data Set under open:
importPackage(javax.xml.parsers);
importPackage(javax.xml.xpath);
importPackage(org.w3c.dom);
importPackage(org.xml.sax);
var factory = DocumentBuilderFactory.newInstance();
var builder = null;
doc = null;
var expr = null;
factory.setNamespaceAware(true);
builder = factory.newDocumentBuilder();
doc = builder.parse(params["FileName"].value);
doc.getDocumentElement().normalize();
// create an XPathFactory
var xFactory = XPathFactory.newInstance();
// create an XPath object
xpath = xFactory.newXPath();
// compile the XPath expression
expr = xpath.compile("/library/*");
// run the query and get a nodeset
var result = expr.evaluate(doc, XPathConstants.NODESET);
//initialize object variables to be used in the fetch method
nodes = result;
x = 0;
y = 0;
cnt = nodes.getLength();
Under fetch:
var lboolAllowed = false;
if (x < cnt)
{
var book = nodes.item(x);
var nodeList = book.getChildNodes();
var nodeListLength = nodeList.getLength();
row["category"] = book.getAttribute("category");
for(int i=0; i<nodeListLength; i++)
{
var child = nodeList.item(i);
var nodeName = child.getNodeName();
var nodeText = child.getTextContent();
if(nodeName == "year")
{
row["year"] = nodeText);
}
else if(nodeName == "info")
{
row["info"] = nodeText);
}
else if(nodeName == "title")
{
row["title"] = nodeText);
}
else if(nodeName == "author")
{
row["author"] = child.getAttribute("name");
row["country"] = child.getAttribute("country");
}
y++
}
x++;
y=0;
return (true);
}
return (false);
Note: author is actually a list
2). You can use esProc as Data Source and use SPL to parse the XML.
Here is the SPL script.
A | |
1 | =file("./book.xml") |
2 | =xml@s(A1.read(),"library/book") |
3 | =A2.library |
4 | =A3.new(category,ifn(book.field("title")).ifn():title,ifn(book.field("lang")).ifn():lang,ifn(book.field("info")).ifn():info, ifn(book.field("name")).select(~!=null).concat@c():name,ifn(book.field("country")).select(~!=null).concat(","):country) |
5 | =A4.new(title,category,(lang,name.array().(~+"[")++country.array().(~+"]")).concat@c():author,info) |
A4:
A5:
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.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
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