Jump to content
We've recently updated our Privacy Statement, available here ×

XMLdataSource


slawrence

Recommended Posts

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 order

Monday

All nodes under there

Tuesday

All nodes under it.

 

How can I achieve this by using subreports. Appreciate your timely help in advance.

 

 

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

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 day

I 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

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

  • 9 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...