在JasperReports中使用XML文件作为数据源的步骤

在JasperReports中使用XML文件作为数据源的步骤

1.报表设计
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="OrdersReport" pageWidth="500" pageHeight="842" columnWidth="500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Bold" isDefault="false" fontName="Arial" fontSize="8" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Italic" isDefault="false" fontName="Arial" fontSize="8" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<parameter name="CustomerID" class="java.lang.String"/>
<queryString language="xPath"><![CDATA[/Northwind/Orders[CustomerID='$P{CustomerID}']]]></queryString>
<field name="Id" class="java.lang.String">
<fieldDescription><![CDATA[OrderID]]></fieldDescription>
</field>
<field name="OrderDate" class="java.lang.String">
<fieldDescription><![CDATA[OrderDate]]></fieldDescription>
</field>
<field name="ShipCity" class="java.lang.String">
<fieldDescription><![CDATA[ShipCity]]></fieldDescription>
</field>
<field name="Freight" class="java.lang.Float">
<fieldDescription><![CDATA[Freight]]></fieldDescription>
</field>
<variable name="TotalFreight" class="java.lang.Float" calculation="Sum">
<variableExpression><![CDATA[$F{Freight}]]></variableExpression>
</variable>
<pageHeader>
<band height="14">
<rectangle>
<reportElement x="0" y="2" width="356" height="10" forecolor="#ccffff" backcolor="#ccffff"/>
<graphicElement/>
</rectangle>
<staticText>
<reportElement mode="Opaque" x="0" y="2" width="48" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="54" y="2" width="87" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Order Date]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="146" y="2" width="108" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<text><![CDATA[Ship City]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="259" y="2" width="92" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Freight]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="14">
<textField>
<reportElement x="0" y="2" width="51" height="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Id}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="54" y="2" width="87" height="10"/>
<textElement textAlignment="Center"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{OrderDate}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="146" y="2" width="108" height="10"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{ShipCity}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="259" y="2" width="92" height="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Float"><![CDATA[$F{Freight}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="14">
<rectangle>
<reportElement x="0" y="2" width="356" height="10" forecolor="#33cccc" backcolor="#33cccc"/>
<graphicElement pen="Thin"/>
</rectangle>
<staticText>
<reportElement mode="Opaque" x="160" y="2" width="67" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Total :]]></text>
</staticText>
<textField>
<reportElement mode="Opaque" x="259" y="2" width="92" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Float"><![CDATA[$V{TotalFreight}]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="227" y="2" width="27" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>


2.报表填充
...
Document document = JRXmlUtils.parse(new File("northwind.xml"));
params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
JasperFillManager.fillReportToFile(fileName, params);
...