JRXML

  • JasperReport is a report with an XML file.
  • Latest release – 6.4.1 on 28 June, 2017 as on 12/12/2017.
  • JasperReports are specifications defined in an XML file format, called
  • It is an open source Java reporting tool that can write to a variety of targets such as a screen, a printer, into pdf, HTML, MS-Excel etc. files.

(RTF – Rich Text Format file, Ex. – MS-Word)

(ODT – Open Document Format file, Ex. – Spreadsheets, Presentations)

  • It can be used in Java-enabled applications including Java EE or Web applications to generate dynamic content. It reads its instructions from .jasper file.
  • .jrxml is a human-readable XML file that contains the report template, report structure and its formatting rules.
  • On compiling the .jrxml file, we get .jasper file which is Java executable and it is used in JVM to generate a report based on your report design.
  • 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)

Some concern the logical characteristics (such as the declaration of the parameters and variables and the definition of a query for data selection).

Let us see the image given below which is a report with some details as shown:

Fig. 1 – How a report looks

 

Now, the sequence in which we have to write the jrxml file for above report is shown below:

———————————————————————————————————-

<Group1>

<Group1 Header> … </ Group1 Header>

<Group1 Footer> … </ Group1 Footer >

</Group1>

<Group2>

<Group2 Header> … </ Group2 Header>

<Group2 Footer> … </ Group2 Footer >

</Group2>

<Details> … </Details>

<Page Header> … </Page Header>

<Page Footer> … </Page Footer>


 

Group (<group>) element in JasperReport helps to organize data on report in a logical manner. A report group has three elements –

  • Group expression− This indicates the data that must change to start a new data group.
  • Group header section− Helps place the label at the beginning of the grouped data. (Optional)
  • Group footer section− Helps place the label at the end of the grouped data. (Optional)

 

Example 1 –

SQL query =>  Select tciOrdrId, tcsItm, tciQty, tcsClientIdy From tabPurchseOrdr ;

This will give a resultset as below:

  Order ID Item Quantity Client
Row 1 1 A 10 SA
Row 2 1 B 20 SA
Row 3 1 C 30 SA
Row 4 2 A 20 KP
Row 5 2 B 30 KP
Row 6 2 C 25 KP
Row 7 2 D 15 KP

 

Say, we made a jrxml with one group i.e. group by tciOrdrId.

JRXML format looks like below –

For given resultset, the JRXML execution flow will be as follow:
Group1 – start

Group1 Expression :  tciOrdrId

Group1 Header – start

staticText: “OrderId” + textField : tciOrdrId + staticText: “Client” + textField : tcsClientIdy

staticText: “Item” + staticText : “Oty”

Group1 Header – end

Group1 Footer – start

staticText: “______________”

staticText: “Total : ” + textField : SUM(tciQty)

Group1 Footer – end

Group1 – end

Details – start

textField : tciOrdrId + textField : tciQty

Details – end

Row 1 => Group1 Header                           (For Order ID as 1)

Details

Row 2 =>                     Details

Row 3 =>                     Details

Group1 Footer

Row 4 => Group1 Header                           (For Order ID as 2)

Details

Row 5 =>                     Details

Row 6 =>                     Details

Row 7 =>                     Details

Group1 Footer

 

Step by Step explanation:

  • tciOrdrId of Row 1 is ‘1’ and being the first row of the resultset, ‘Group header’ will be executed and then followed by ‘Details’.
  • tciOrdrId of Row 2 is also ‘1’, hence it is part of the existing group. So, only ‘Details’ block will execute.
  • tciOrdrId of Row 3 is also ‘1’, hence it is part of the existing group. Also, row 3 is the last row with tciOrdrId as ‘1’, hence along with ‘Details’, ‘Group footer’ will also execute.
  • tciOrdrId of Row 4 is ‘2’ which means a new group is starting. So, ‘Group header’ will be executed and then followed by ‘Details’.
  • tciOrdrId of Row 5 is also ‘2’, hence it is part of the existing group. So, only ‘Details’ block will execute.
  • tciOrdrId of Row 6 is also ‘2’, hence it is part of the existing group. So, only ‘Details’ block will execute.
  • tciOrdrId of Row 7 is also ‘2’, hence it is part of the existing group. Also, row 7 is the last row with tciOrdrId as ‘2’, hence along with ‘Details’, ‘Group footer’ will also execute.

As a result, the report will be generated as below:

Fig. 2 – Report for example

 

Example 2 –

For the following resultset, there will some query written in the queryString element.

  Order ID Item Shipping Location Qty Client
Row 1 1 A L1 10 SA
Row 2 1 A L2 10 SA
Row 3 1 A L3 20 SA
Row 4 1 B L1 30 SA
Row 5 1 B L3 10 SA
Row 6 2 A L1 20 KP
Row 7 2 A L2 10 KP
Row 8 2 B L3 30 KP

 

Say, we made a jrxml with two group i.e group by Order ID and Item. JRXML format looks like below –

 

For above resultset, the jrxml flow will be as:

 

Group1 – start

Group1 Expression :  tciOrdrId

Group1 Header – start

staticText: “OrderId” + textField : tciOrdrId + staticText: “Client” + textField : tcsClientIdy

Group2 Header – start

staticText: “Item” + textField : tcsItm

staticText : “Loc” + staticText : “Oty”

Group2 Header – end

Group2 Footer – start

staticText: “______________”

staticText: “Total : ” + textField : SUM(tciQty)

Group2 Footer – ends

Group1 Header – end

Group1 Footer – start

staticText: “Total for Order ID :” + textField : tciOrdrId +

textField : SUM(Total)

Group1 Footer – end

Group1 – end

Details – start

textField : tcsLoc + textField : tciQty

Details – end

Page Header – start

StaticText: “Report”

Page Header – end

Page Footer – start

staticText: “Page No.” + textField : Pg. No.

Page Footer – end

Page Header (Optional)

Row 1 => Group1 Header, Group2 Header, Details (For Order ID as 1 and Item as A)

Row 2 => Details

Row 3 => Details, G2 Footer

Row 4 => Group2 Header, Details                 (For Item as B)

Row 5 => Details, Group2 Footer, Group1 Footer

Row 6 => Group1 Header, Group2 Header, Details       (For Order ID as 2 and Item as A)

Row 7 =>Details, Group2 Footer

Row 8 =>Group2 Header, Details, Group2 Footer, Group1 Footer                                                            (For Item as B)

Page Footer (optional)

 

 

Step by Step explanation:

  • tciOrdrId of Row 1 is ‘1’and tcsItm is ‘A’ and being the first row of the resultset, ‘Group1 header’ (group by order ID) and ‘Group2 header‘ (group by item) will be executed and then followed by ‘Details’.
  • tciOrdrId of Row 2 is also ‘1’ and tcsItm is also ‘A’, hence it is part of existing group1 and group2. So, only ‘Details’ block will execute.
  • tciOrdrId of Row 3 is also ‘1’ and tcsItm is ‘A’, hence it is part of existing group1 and group2. Also, row 3 is the last row with tcsItm as ‘A’, hence along with ‘Details’, ‘Group2 footer’ will also execute.
  • tciOrdrId of Row 4 is ‘1’ and tcsItm is ‘B’ which means it is part of existing group1 and a new group2 is starting. So, ‘Group2 header’ will be executed and then followed by ‘Details’.
  • tciOrdrId of Row 5 is also ‘1’ and tcsItm is ‘B’, hence it is part of exiting group1 and group2. Also, row 5 is the last row with tcsItm as ‘B’ and tciOrdrId as ‘1’, hence along with ‘Details’, ‘Group2 footer’ and ‘Group1 footer’will also execute.
  • tciOrdrId of Row 6 is ‘2’ and tcsItm is ‘A’ which means two new group (group1 & group2) will be starting. So, ‘Group1 header’, ‘Group2 header’ will be executed and then followed by ‘Details’.
  • tciOrdrId of Row 7 is ‘2’ and tcsItm is also ‘A’, hence it is part of existing group1 and group2. Also, row 7 is the last with tcsItm as ‘A’, hence along with ‘Details’, ‘Group2 footer’ will also execute.
  • tciOrdrId of Row 8 is ‘2’ and tcsItm is ‘B’ which means it is part of existing group1 and a new group2 is starting. Also, row 8 is the last row with tcsItm as ‘A’ and tciOrdrId as ‘2’. So, along ‘Group2 header’, ’Details’, ‘Group2 footer’ and ‘group1 footer’ will execute.

 

As a result, the report will be generated as below:

Fig. 3 – Report for example 2

 

Some more elements

  • Basic element attributes
    1. Appearance– allows you to set the location, size, color, and text style of the field
    2. Borders– allows you to set the padding and border style, color, and width of the field
    3. Static Text – allows you to define unchangeable text for a field, and control its appearance
    4. Text Field – allows you to place text in an element
    5. Inheritance– allows you to view any attributes inherited from another level, and override those attributes when possible
    6. Hyperlink– allows you to define a hyperlink in an element
    7. Advanced– displays detailed information about the element

 

  • Positioning elements
    1. Bands – The top and left values that define the element’s position are always relative to the parent container, which is usually a band but can be a frame element.

An element must remain in its parent band; it should not be moved even partially out of the band.

 

  • Frame elements

A frame is an element that can contain other elements and optionally draw a border around them. A frame can contain other frames, and so recursively.

 

  • Field element

Fields (<field>) are elements, which represent a mapping of data between datasource and report template. Fields can be combined in the report expressions to obtain the desired output.

When declaring report fields, the data source should supply data corresponding to all the fields defined in the report template.

Field declaration   <field name = “FieldName” class = “java.lang.String”/>

Field attributes

  1. name – The name attribute of field element is mandatory. It references the field in report expressions by name.
  2. class – The class attribute specifies the class name for the field values. Its default value is java.lang.String. This can be changed to any class available at runtime.

Field Description The fieldDesciption (<fieldDesciption>) element is an optional element. This is very useful when implementing a custom data source. For example, we can store a key or some information, by which we can retrieve the value of a field from the custom data source at runtime. By using this element instead of the field name, you can easily overcome restrictions of field-naming conventions when retrieving the field values from the data source.

  • Group

Group attributes

  1. name – This is mandatory. It references the group in report expressions by name. It follows the same naming conventions that we mentioned for the report parameters, fields, and report variables. It can be used in other JRXML attributes when you want to refer a particular report group.
  2. isStartNewColumn – When set to true, each data group will begin on a new column. The default value is false.
  3. isStartNewPage – When set to true, each data group will begin on a new page. The default value is false.
  4. isResetPageNumber – When set to true, the report page number will be reset every time a new group starts. The default value is false.
  5. isReprintHeaderOnEachPage – When set to true, the group header will be reprinted on every page. The default value is false.
  6. minHeightToStartNewPage – Defines the minimum amount of vertical space needed at the bottom of the column in order to place the group header in the current column. The amount is specified in report units.
  7. footerPosition – Renders position of the group footer on the page, as well as its behavior in relation to the report sections that follow it. Its value can be: Normal, StackAtBottom, ForceAtBottom, and CollateAtBottom. The default value is Normal.
  8. keepTogether – When set to true, prevents the group from splitting on its first break attempt.

 

  • Style element

Style (<style>) element helps to control text properties in a report template. This element is a collection of style settings declared at the report level. Styles can extend other styles, and add to, or override properties of the parent style as well.

Style attributes

  1. name – Is mandatory. It must be unique because it references the corresponding report style throughout the report.
  2. isDefault – Indicates whether this style is the document’s default style.
  3. style – Is a reference to the parent style.
  4. mode – Specifies the element’s transparency. Possible values are Opaque and Transparent.
  5. forecolor – Is the foreground color of an object.
  6. backcolor – Is the background color of an object.
  7. fill – Determines the fill pattern used to fill the object. At the moment, the single value allowed is Solid.
  8. radius – Specifies the radius of the rectangle’s corner arc.
  9. scaleImage – Specifies scale for the images only. Possible values: Clip, FillFrame, RetainShape, RealHeight, and RealSize.
  10. hAlign – Specifies the horizontal alignment. Possible values: Left, Center, Right, and Justified.
  11. vAlign – Specifies the vertical alignment. Possible values: Top, Middle, and Bottom.
  12. rotation – Specifies the element’s rotation. Possible values: None, Left, Right,and UpsideDown.
  13. lineSpacing – Specifies the line spacing between lines of text. Possible values:Single, 1_1_2, Double.
  14. Markup – Specifies the markup style for styled texts.
  15. fontName – Specifies the font name.
  16. fontSize – Specifies the font size.
  17. isBold – Indicates if the font style is bold.
  18. isItalic – Indicates if the font style is italic.
  19. isUnderline – Indicates if the font style is underline.
  20. isStrikeThrough – Indicates if the font style is strikethrough.

 

  • QueryString

QueryString (<queryString>) is an element in which we write our SQL query whose result we have to show in the report.

 

  • ReportElement

ReportElement (<reportElement>) is an element which is first element used in staticText, textField, subReport, image etc. elements. It determines how data is laid out for that particular element.

ReportElement attributes –

  1. x – Specifies the x coordinate of the band element.
  2. y – Specifies the y coordinate of the band element.
  3. width – Specifies the y coordinate of the band element.
  4. height – Specifies the height of the band element.
  5. key – Unique identifier of band element.
  6. stretchType – Specifies how does the element stretch when the containing band stretches. Values are NoStretch (default), RelativeToTallestObject and RelativeToBand.
  7. positionType – Specifies the element’s position when the band stretches.
  8. isPrintRepeatedValues – Specifies if repeated values are printed.
  9. mode – Specifies if repeated values are printed.

 

 

 

 

 

 

 

 

 

 

 

For more understanding, go through a live example of the jrxml file along with report…

This small concept is given by S AnAnD

Share it
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •