I am trying to generate a report from XML file using xml data source. However generated report has only one row. Whereas input file has 4 values.
My jrxml file is
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="simpleReport"> <queryString language="xPath"><![CDATA[/response/results]]></queryString> <field name="field" class="java.lang.String"> <fieldDescription><![CDATA[//field]]></fieldDescription> </field> <field name="count" class="java.lang.String"> <fieldDescription><![CDATA[//field[@count]/@count]]></fieldDescription> </field> <title> <band height="50"> <staticText> <reportElement x="0" y="0" width="180" height="15"/> <textElement/> <text><![CDATA[Report]]></text> </staticText> </band> </title> <pageHeader> <band/> </pageHeader> <columnHeader> <band height="20"> <staticText> <reportElement x="180" y="0" width="180" height="20"/> <textElement> <font isUnderline="true"/> </textElement> <text><![CDATA[Event Name]]></text> </staticText> <staticText> <reportElement x="360" y="0" width="180" height="20"/> <textElement> <font isUnderline="true"/> </textElement> <text><![CDATA[Count]]></text> </staticText> </band> </columnHeader> <detail> <band height="20"> <textField> <reportElement x="180" y="0" width="180" height="15"/> <textElement/> <textFieldExpression><![CDATA[$F{field}]]></textFieldExpression> </textField> <textField> <reportElement x="360" y="0" width="180" height="15"/> <textElement/> <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band/> </columnFooter> <pageFooter> <band height="15"> <staticText> <reportElement x="0" y="0" width="40" height="15"/> <textElement/> <text><![CDATA[Page:]]></text> </staticText> <textField> <reportElement x="40" y="0" width="100" height="15"/> <textElement/> <textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression> </textField> </band> </pageFooter> <summary> <band/> </summary> </jasperReport>
input xml file is
<response id="1074200577"> <results id1="0" id2="0"> <field count="7556">one</field> <field count="7524">two</field> <field count="7402">three</field> <field count="7304">four</field> </results> </response>
My Java code looks like below
JasperCompileManager.compileReportToFile(inpuutjrxml, outputjasper); JRXmlDataSource source = new JRXmlDataSource(new File(sourceFile)); HashMap<String, Object> params = new HashMap<String, Object>(); JasperPrint jasperPrint = JasperFillManager.fillReport(outputjasper, params, source); JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pefoutput));
Is it a xpath issue or some modification needs to be done for jrxml file when we use xml data source?
2 Answers:
Posted on August 9, 2013 at 1:27am
Passing the xml path while creating the data source fixed the issue.
Used following constructor to create a new xml data source
JRXmlDataSource source = new JRXmlDataSource(new File(file,"/response/results/field");
Then removed query String from jrxml. Modified field section looks like below
<field name="field" class="java.lang.String"> <fieldDescription><![CDATA[child::text()]]></fieldDescription> </field> <field name="count" class="java.lang.String"> <fieldDescription><![CDATA[@count]]></fieldDescription> </field>
Posted on August 9, 2013 at 12:25am
As I test with my own xml with the same format as yours, it works fine. I think it may from your xml file, but I not sure.
This is my xml file:
<?xml version="1.0" encoding="utf-8"?> <customers d="00001"> <customer type1="A" type2="A"> <person id="1">A</person> <person id="2">B</person> <person id="3">C</person> <person id="4">D</person> </customer> </customers>
This is my report code:
<?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="dataxml" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ef5c8faf-8453-4ad5-8e1e-0eb8441c05bf"> <property name="ireport.zoom" value="0.8264462809917354"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <queryString language="xPath"> <![CDATA[/customers/customer/person]]> </queryString> <field name="id" class="java.lang.String"> <fieldDescription><![CDATA[@id]]></fieldDescription> </field> <field name="person" class="java.lang.String"> <fieldDescription><![CDATA[child::text()]]></fieldDescription> </field> <background> <band splitType="Stretch"/> </background> <columnHeader> <band height="24"> <staticText> <reportElement uuid="2e20d0c1-2c6e-45fd-a655-beccf4e665a4" x="0" y="0" width="277" height="20"/> <textElement/> <text><![CDATA[child::text()]]></text> </staticText> <staticText> <reportElement uuid="c9cde1b4-929b-4ab7-92ae-0c4f6b711d20" x="277" y="0" width="277" height="20"/> <textElement/> <text><![CDATA[@id]]></text> </staticText> </band> </columnHeader> <detail> <band height="39" splitType="Stretch"> <textField> <reportElement uuid="b45481e9-c7bd-4189-bba8-893a64a976cd" x="0" y="0" width="277" height="20"/> <textElement/> <textFieldExpression><![CDATA[$F{person}]]></textFieldExpression> </textField> <textField> <reportElement uuid="52e2bfe1-616b-4da7-bdeb-4bc33d959c1d" x="277" y="0" width="277" height="20"/> <textElement/> <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression> </textField> </band> </detail> <summary> <band height="42" splitType="Stretch"/> </summary> </jasperReport>