Home
Analytics
BIRT scripted ds with dynamic xml data attributes
suresh@anumolu
<p>Hi, </p>
<p> </p>
<p>Can someone please guide me on how to create scripted ds using an dynamically generated xml file. </p>
<p>The columns needs to be generated dynamically because xml file will be different for each run. </p>
<p>I will not be able to use XML data source as the file is different each run, i need to set the column format inn the script based on the xml attribute. </p>
<p> </p>
<p>Also what kind of xml package would i be able to use with in the scripted DS. </p>
<p>Any help on this would be appreciated.</p>
<p> </p>
<p>thanks for your help.</p>
<p> </p>
<p>Regards</p>
<p>suresh</p>
Find more posts tagged with
Comments
Clement Wong
<p>Suresh,</p>
<p> </p>
<p>It is possible to use a Scripted Data Source / Data Set using a dynamically generated XML. You can parse it with E4X (see this DevShare @ <a data-ipb='nomediaparse' href='
http://developer.actuate.com/community/forum/index.php?/files/file/1122-parsing-xml-in-birt-with-e4x/).'>http://developer.actuate.com/community/forum/index.php?/files/file/1122-parsing-xml-in-birt-with-e4x/).</a></p>
;
<p> </p>
<p>Do you have sample XML to share with us?</p>
<p> </p>
<p>How complex is the resulting report? Do you have a sample report output?</p>
<p> </p>
<p>You will most likely need to read your XML in the beforeFactory so that you can build your report items dynamically. Then in your Scripted Data Set, you can reuse the XML variable (without going back to the file or URL) to populate your report.</p>
suresh@anumolu
<p>Thanks Wong. I looked at the E4X example, that takes an xml string. where as i am trying to load a xml file saved on server. Can you please help he on what xml package should i be using to load xml file and apply filters and xpath?</p>
<p> </p>
<p>i am still working on my report format, but my report will contains a grid and chart, and regarding xml file- it just bunch xml attributes with nuermical fields. something like below, but thousands of these nodes</p>
<p> </p>
<div><LoanBucket ActiveLoanCount="7578384.0000000000" AdjCPR="24.3149323094" AdjSMM="2.2948362405" AdjSchedBal="1168014167280.8232421875" Age="32.1679251540" AvgLoanSize="172090.2202816079" BzDays="20.0000000000" CPR="23.2906793438" ClusterName="FactorDate_Bucket:20130101;" Coupon="4.3389597279" CurrentCoupon="3.3275000000" FICO="696.1292127715" FactorDate="20130101.0000000000" GrossCpn="4.7516660292" KEY="FactorDate_Bucket:20130101;" LTV="94.3553707481" NumberOfLoans="7661539.0000000000" OrigBal="3190002133602.5810546875" PoolAge="31.0920660327" PoolCount="201910.0000000000" PreviousBalance="1168506236547.0415039062" RefiIncentive="1.4241660292" RemTerm="323.9049130155" SATO="0.0539873305" SMM="2.1853261013" ScheduledBalance="1166706496468.9189453125" UPB="1141210154876.2087402344" UPB_Million="1141210.1548762086" Vintage="2009.0000000000"/></div>
Clement Wong
<p>The E4X DevShare example shows the technique of using the XML as a native data type. You can read in the XML as a URL (illustrated in the DevShare), or from a file... it's up to you.</p>
<p> </p>
<p>To address your question as to how you can read a XML file, it's your choice of Java packages that you want to use. Examples of reading in a file using BIRT:</p>
<p> </p>
<p><a data-ipb='nomediaparse' href='
https://www.eclipse.org/forums/index.php?t=msg&th=118614&goto=360933&#msg_360933'>https://www.eclipse.org/forums/index.php?t=msg&th=118614&goto=360933&#msg_360933</a></p>
;
<p><a data-ipb='nomediaparse' href='
http://developer.actuate.com/community/forum/index.php?/topic/15591-how-can-i-read-a-text-file-from-birt/'>http://developer.actuate.com/community/forum/index.php?/topic/15591-how-can-i-read-a-text-file-from-birt/</a></p>
;
<p><a data-ipb='nomediaparse' href='
http://developer.actuate.com/community/forum/index.php?/topic/22525-embedding-external-html-files-in-report/?p=78173'>http://developer.actuate.com/community/forum/index.php?/topic/22525-embedding-external-html-files-in-report/?p=78173</a></p>
;
suresh@anumolu
<p>Thanks for the information Wong. I am able load using File handle. And when i checked for E4X i noticed that its being discontinued. Is there any link you can provide me to load the Xml DomParser as i am noticing an error missing ; with DocumentBuilder?</p>
<p> </p>
<p><span style="margin:0px;font-weight:700;color:rgb(77,78,83);font-family:'Open Sans', Arial, sans-serif;background-color:rgb(250,215,212);">Warning:</span><span style="color:rgb(77,78,83);font-family:'Open Sans', Arial, sans-serif;font-size:12px;background-color:rgb(250,215,212);"> E4X is obsolete. It has been disabled by default for webpages (content) in Firefox 17, disabled by default for chrome in Firefox 20, and has been removed in Firefox 21. Use DOMParser/DOMSerializer or a non-native JXON algorithm instead.</span></p>
<p> </p>
<p>I tried to use the following methods to replace E4X with </p>
<p>DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();</p>
<div>DocumentBuilder builder = factory.newDocumentBuilder();</div>
<div>Document doc = builder.parse(input);</div>
suresh@anumolu
<div>Error missing ; after importPackage statements. Can you please let me know what i am missing here?</div>
<div>importPackage (Packages.org.w3c.dom.*);</div>
<div>importPackage (Packages.javax.xml.parsers.*);</div>
<div>importPackage (Packages.java.io );</div>
<div> </div>
<div>
<p style="color:rgb(40,40,40);font-family:'Source Sans Pro', sans-serif;">DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();</p>
<div style="margin:0px;color:rgb(40,40,40);font-family:'Source Sans Pro', sans-serif;">DocumentBuilder builder = factory.newDocumentBuilder();</div>
<div style="margin:0px;color:rgb(40,40,40);font-family:'Source Sans Pro', sans-serif;">Document doc = builder.parse(input);</div>
<div> </div>
</div>
Clement Wong
<p>Sorry for the late reply, I was out on vacation last week.</p>
<p> </p>
<p>The equivalent of this example @ <a data-ipb='nomediaparse' href='
http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/'>http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/</a>in
BIRT would be coded like the following:</p>
<pre class="_prettyXprint">
importPackage(Packages.java.io);
importPackage(Packages.javax.xml.parsers);
importPackage(Packages.org.xml.sax);
var result = new java.lang.String("<book><person><first>Kiran</first><last>Pai</last><age>22</age></person><person><first>Bill</first><last>Gates</last><age>46</age></person><person><first>Steve</first><last>Jobs</last><age>40</age></person></book>");
var docBuilderFactory = DocumentBuilderFactory.newInstance();
var docBuilder = docBuilderFactory.newDocumentBuilder();
var stream = ByteArrayInputStream(result.toString().getBytes("utf-8"));
var doc = docBuilder.parse (stream);
var listOfPersons = doc.getElementsByTagName("person");
var totalPersons = listOfPersons.getLength();
//FOR DEBUG, THIS WORKS ONLY IN COMMERCIAL BIRT
logger = java.util.logging.Logger.getLogger("birt.report.logger");
logger.warning ("Total no of people : " + totalPersons);
</pre>
<p>I tested this out in commercial BIRT (iHub 3.1) and the output was as expected. 3 total people were returned.</p>
suresh@anumolu
<p>Wong,</p>
<p>Sorry about the delayed response. your eg worked fine. Can you please tell me what package should i be importing to cast the getElementsByTagName() response to a NodeList and similarly if i wanted to use getAttribute() or Attributes["name"] functionality here in BIRT.</p>
<p> </p>
<p>some thing like</p>
<p>NodeList listOfPersons = doc.getElementsByTagName("person");<br>
Element firstPersonNode = (Element)listOfPersons.item(1);</p>
<p>firstPersonNode.getAttribute("Name")</p>
<p> </p>
<p>thanks for your help.</p>
Clement Wong
<p>You don't need to cast since we are using the Rhino engine.</p>
<p> </p>
<p>So using the Java example again (<a data-ipb='nomediaparse' href='
http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/'>http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/</a>)
, we are going to print out the total # of persons, and each person's first name.</p>
<pre class="_prettyXprint">
importPackage(Packages.java.io);
importPackage(Packages.javax.xml.parsers);
importPackage(Packages.org.xml.sax);
var result = new java.lang.String("<book><person><first>Kiran</first><last>Pai</last><age>22</age></person><person><first>Bill</first><last>Gates</last><age>46</age></person><person><first>Steve</first><last>Jobs</last><age>40</age></person></book>");
var docBuilderFactory = DocumentBuilderFactory.newInstance();
var docBuilder = docBuilderFactory.newDocumentBuilder();
var stream = ByteArrayInputStream(result.toString().getBytes("utf-8"));
var doc = docBuilder.parse (stream);
var listOfPersons = doc.getElementsByTagName("person");
var totalPersons = listOfPersons.getLength();
//FOR DEBUG, THIS WORKS ONLY IN COMMERCIAL BIRT
logger = java.util.logging.Logger.getLogger("birt.report.logger");
logger.warning ("Total no of people : " + totalPersons);
for (var s=0; s<listOfPersons.getLength() ; s++){
var firstPersonNode = listOfPersons.item(s);
if (firstPersonNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
var firstPersonElement = firstPersonNode;
var firstNameList = firstPersonElement.getElementsByTagName("first");
var firstNameElement = firstNameList.item(0);
var textFNList = firstNameElement.getChildNodes();
logger.warning ("First Name : " + (textFNList.item(0)).getNodeValue().trim());
}
} </pre>
<p>The output from the sample code:</p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;">Total no of people : 3</span></p>
<p><span style="font-family:'courier new', courier, monospace;">First Name : Kiran</span></p>
<p><span style="font-family:'courier new', courier, monospace;">First Name : Bill</span></p>
<p><span style="font-family:'courier new', courier, monospace;">First Name : Steve</span></p>
<p> </p>
<p> </p>
<p><span style="font-family:'courier new', courier, monospace;"><span style="font-family:arial, helvetica, sans-serif;">BTW, Clement is my first name.</span></span></p>
suresh@anumolu
<p>Thanks for the prompt response Clement. i will take a look at this. <span style="font-family:arial, helvetica, sans-serif;"><span style="font-size:12px;"><span style="color:rgb(0,0,0);"><strong> </strong></span></span></span></p>