slawrence Posted May 8, 2009 Share Posted May 8, 2009 Hi All, I'm new to jasper. need to use xmldatasource for reading input from xml file. The input xml looks like --<?xml version="1.0" encoding="UTF-8"?><A id="tt"> <B id="Monday"> <C> <D>2009-03-04</D> <E>502</E> </C> <F> <D>2009-03-04</D> <E>502</E> </F> <G id="kk"> <D>2009-03-04</D> <E>502</E> </G> <G id="ii"> <D>2009-03-04</D> <E>502</E> </G> </B> <B id="tuesday"> <C> <D>2008-03-04</D> <E>502</E> </C> <F> <D>2008-02-04</D> <E>502</E> </F> <G id="kk2"> <D>2008-01-04</D> <E>502</E> </G> <G id="ii2"> <D>2009-03-06</D> <E>502</E> </G> </B> I need the report to list in the following orderMondayAll nodes under thereTuesdayAll nodes under it. How can I achieve this by using subreports. Appreciate your timely help in advance. Link to comment Share on other sites More sharing options...
andfedotov Posted May 9, 2009 Share Posted May 9, 2009 Hi!1. As I understood from your xml data source you probably need to consider changing it a bit because you might have different number of C element occurrences...Will this one work for you? I attached it as data1.xml file<?xml version="1.0" encoding="UTF-8"?><A id="tt"> <B id="Monday"> <C> <D>2009-03-04</D> <E>502</E> </C> <C> <D>2009-03-04</D> <E>502</E> </C> <C id="kk"> <D>2009-03-04</D> <E>502</E> </C> <C id="ii"> <D>2009-03-04</D> <E>502</E> </C> </B> <B id="tuesday"> <C> <D>2008-03-04</D> <E>502</E> </C> <C> <D>2008-02-04</D> <E>502</E> </C> <C id="kk2"> <D>2008-01-04</D> <E>502</E> </C> <C id="ii2"> <D>2009-03-06</D> <E>502</E> </C> </B></A>2. You need to create Master report where you'll be iterating over days of the week (element B) and include Subreport where you'll be iterating over all C's that you have in this dayI provided a master's datasource to the subreport with this expression:((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/A/B[@id="+$F{WeekDay}.toString()+"]/C")Note: this will work only in case your B element have different id's cause a subreport's datasource rely on uniqueness of B element Hope this will help!Regards, Andriy Link to comment Share on other sites More sharing options...
andfedotov Posted May 10, 2009 Share Posted May 10, 2009 The other way to do it is the new feature that comes with 3.5.1 version called "List Component".It'll make the job done in your case.Look at the attachmentsDatasource select expression is: /A/B Link to comment Share on other sites More sharing options...
andfedotov Posted May 15, 2009 Share Posted May 15, 2009 Even better version that uses subDataSource method instead of dataSource that frees you from the mandatory of using id attribute for your xml nodes <subDataset name="dataset1"> <queryString language="xPath"> <![CDATA[]]> </queryString> <field name="C_id" class="java.lang.String"> <fieldDescription><![CDATA[./@id]]></fieldDescription> </field> <field name="D" class="java.lang.String"> <fieldDescription><![CDATA[D]]></fieldDescription> </field> <field name="E" class="java.lang.String"> <fieldDescription><![CDATA[E]]></fieldDescription> </field> </subDataset> <componentElement> <reportElement x="31" y="31" width="400" height="20"/> <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd"> <datasetRun subDataset="dataset1"> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/B/C")]]></dataSourceExpression> </datasetRun> <jr:listContents height="20"> <textField isBlankWhenNull="true"> <reportElement x="13" y="0" width="100" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{C_id}]]></textFieldExpression> </textField> <textField> <reportElement x="127" y="0" width="100" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{D}]]></textFieldExpression> </textField> <textField> <reportElement x="246" y="0" width="100" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{E}]]></textFieldExpression> </textField> </jr:listContents> </jr:list> </componentElement> Link to comment Share on other sites More sharing options...
ollywatkins Posted March 4, 2010 Share Posted March 4, 2010 Many many many thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!This is the only IReport example, that has worked for me, that shows how to do subreports from XML Datasource.Remember to put in "/A/B" into the report query. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now