JRXML Sources and Jasper Files

JasperReports defines a report with an XML file. A jrxml file is composed of a set of sections; some concern the report’s physical characteristics (such as the dimension of the page, the positioning of the fields, and the height of the bands), and some concern the logical characteristics (such as the declaration of the parameters and variables and the definition of a query for data selection).

The following figure shows sample report source code:

A simple JRMXL file example

<a name="kanchor14"></a><?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports 
        http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
      name="My first report" pageWidth="595" pageHeight="842" columnWidth="535" 
      leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
  <queryString language="SQL">
    <![CDATA[select * from address order by city]]>
  </queryString>
  <field name="ID" class="java.lang.Integer">
    <fieldDescription><![CDATA[]]></fieldDescription>
  </field>
  <field name="FIRSTNAME" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
  </field>
  <field name="LASTNAME" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
  </field>	
  <field name="STREET" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
  </field>
  <field name="CITY" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
  </field>	
  <group name="CITY">
    <groupExpression><![CDATA[$F{CITY}]]></groupExpression>
    <groupHeader>
      <band height="27">
        <staticText>
          <reportElement mode="Opaque" x="0" y="0" width="139" height="27" 
          forecolor="#FFFFFF" backcolor="#000000"/>
          <textElement>
            <font size="18"/>
          </textElement>
        <text><![CDATA[CITY]]></text>
      </staticText>
      <textField hyperlinkType="None">
        <reportElement mode="Opaque" x="139" y="0" width="416" height="27" 
        forecolor="#FFFFFF" backcolor="#000000"/>
        <textElement>
          <font size="18" isBold="true"/>
        </textElement>
        <textFieldExpression class="java.lang.String"><![CDATA[$F{CITY}]]>
        </textFieldExpression>
      </textField>
    </band>
  </groupHeader>
  <groupFooter>
    <band height="8">
      <line direction="BottomUp">
        <reportElement key="line" x="1" y="4" width="554" height="1"/>
      </line>
    </band>
  </groupFooter>
</group>
 
<background>
  <band/>
</background>
  <title>
    <band height="58">
      <line>
        <reportElement x="0" y="8" width="555" height="1"/>
      </line>
      <line>
        <reportElement positionType="FixRelativeToBottom" x="0" y="51" width="555" 
             height="1"/>
      </line>
 
      <staticText>
        <reportElement x=65” y=13” width ”424” height=35/>
        <textElement textAlignment=”Center”>
          <font size=26” isBold=true/>
        </textElement>
      <text><![CDATE[Classic template]]> </text>
    </staticText>
</band>
</title>
  <pageHeader>
    <band/>
  </pageHeader>
  <columnHeader>
    <band height="18">
      <staticText>
        <reportElement mode="Opaque" x="0" y="0" width="138" height="18" 
        forecolor="#FFFFFF" backcolor="#999999"/>
        <textElement>
          <font size="12"/>
        </textElement>
        <text><![CDATA[ID]]></text>
      </staticText>
      <staticText>
        <reportElement mode="Opaque" x="138" y="0" width="138" height="18" 
        forecolor="#FFFFFF" backcolor="#999999"/>
        <textElement>
          <font size="12"/>
        </textElement>
        <text><![CDATA[FIRSTNAME]]></text>
      </staticText>
      <staticText>
        <reportElement mode="Opaque" x="276" y="0" width="138" height="18" 
        forecolor="#FFFFFF" backcolor="#999999"/>
        <textElement>
          <font size="12"/>
        </textElement>
        <text><![CDATA[LASTNAME]]></text>
      </staticText>
      <staticText>
        <reportElement mode="Opaque" x="414" y="0" width="138" height="18" 
        forecolor="#FFFFFF" backcolor="#999999"/>
        <textElement>
          <font size="12"/>
        </textElement>
        <text><![CDATA[STREET]]></text>
      </staticText>
    </band>
  </columnHeader>
  <detail>
    <band height="20">
      <textField hyperlinkType="None">
        <reportElement x="0" y="0" width="138" height="20"/>
        <textElement>
          <font size="12"/>
        </textElement>
        <textFieldExpression class="java.lang.Integer"><![CDATA[$F{ID}]]>
        </textFieldExpression>
      </textField>
      <textField hyperlinkType="None">
        <reportElement x="138" y="0" width="138" height="20"/>
      </textField>
        <textElement>
          <font size="12"/>
        </textElement>
        <textFieldExpression class="java.lang.String"><![CDATA[$F{FIRSTNAME}]]>
        </textFieldExpression>
      <textField hyperlinkType="None">
        <reportElement x="276" y="0" width="138" height="20"/>
        <textElement>
          <font size="12"/>
        </textElement>
        <textFieldExpression class="java.lang.String"><![CDATA[$F{LASTNAME}]]>
        </textFieldExpression>
      </textField>
      <textField hyperlinkType="None">
        <reportElement x="414" y="0" width="138" height="20"/>
        <textElement>
          <font size="12"/>
        </textElement>
        <textFieldExpression class="java.lang.String"><![CDATA[$F{STREET}]]>
        </textFieldExpression>
      </textField>
    </band>
  </detail>
  <columnFooter>
    <band/>
  </columnFooter>
  <pageFooter>
    <band height="26">
      <textField evaluationTime="Report" pattern="" isBlankWhenNull="false" 
      hyperlinkType="None">
        <reportElement key="textField" x="516" y="6" width="36" height="19" 
        forecolor="#000000" backcolor="#FFFFFF"/>
        <textElement>
          <font size="10"/>
        </textElement>
        <textFieldExpression class="java.lang.String"><![CDATA["" + 
        $V{PAGE_NUMBER}]]></textFieldExpression>
      </textField>
      <textField pattern="" isBlankWhenNull="false" hyperlinkType="None">
        <reportElement key="textField" x="342" y="6" width="170" height="19" 
        forecolor="#000000" backcolor="#FFFFFF"/>
        <box>
          <topPen lineWidth="0.0" line lineColor="#000000"/>
          <leftPen lineWidth="0.0" line lineColor="#000000"/>
          <bottomPen lineWidth="0.0" line lineColor="#000000"/>
          <rightPen lineWidth="0.0" line lineColor="#000000"/>
        </box>
        <textElement textAlignment="Right">
          <font size="10"/>
        </textElement>
        <textFieldExpression class="java.lang.String"><![CDATA["Page " + 
        $V{PAGE_NUMBER} + " of "]]></textFieldExpression>
      </textField>
      <textField pattern="" isBlankWhenNull="false" hyperlinkType="None">
        <reportElement key="textField" x="1" y="6" width="209" height="19" 
        forecolor="#000000" backcolor="#FFFFFF"/>
        <box>
          <topPen lineWidth="0.0" line lineColor="#000000"/>
          <leftPen lineWidth="0.0" line lineColor="#000000"/>
          <bottomPen lineWidth="0.0" line lineColor="#000000"/>
          <rightPen lineWidth="0.0" line lineColor="#000000"/>
        </box>
        <textElement>
          <font size="10"/>
        </textElement>
        <textFieldExpression class="java.util.Date"><![CDATA[new Date()]]>
        </textFieldExpression>
      </textField>
    </band>
  </pageFooter>
  <summary>
    <band/>
  </summary>
</jasperReport>

During compilation of the jrxml file (using some JasperReports classes), the XML is parsed and loaded in a JasperDesign object, which is a rich data structure that allows you to represent the exact XML contents in memory. Regardless of which language is used for expressions inside the jrxml, JasperReports creates a special Java class that represents the whole report. The report is then compiled, instanced, and serialized in a jasper file, ready for loading at any time.

JasperReports' speedy operation is due to all of a report’s formulas being compiled into Java-native bytecode and the report structure being verified during compilation instead of at run time. The jasper file does not contain extraneous resources, such as images used in the report, resource bundles to run the report in different languages, or extra scriptlets and external style definitions. All these resources must be provided by the host application and located at run time.

Version: 
Feedback