2006 IR Open Dicussion Posted August 27, 2006 Share Posted August 27, 2006 By: rhyme 123456 - rhymeqazwsx XML Data Source 2006-04-07 02:07 I'm having trouble getting the AddressBook example to work. The main report shows the person details correctly but I can't get the sub-report to display each person's email address correctly. The right number of entries are shown but the first e-mail address for each person is repeated. Strangely when I write a noddy to examine the XMLDataSource it all comes out fine. Here are the report designs Main Report <?xml version="1.0" encoding="UTF-8" ?> <!-- Created with iReport - A designer for JasperReports --> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="AddressBook" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842" columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages" isTitleNewPage="false" isSummaryNewPage="false"> <property name="ireport.scriptlethandling" value="0" /> <property name="ireport.encoding" value="UTF-8" /> <import value="java.util.*" /> <import value="net.sf.jasperreports.engine.*" /> <import value="net.sf.jasperreports.engine.data.*" /> <parameter name="EMailSubReport" isForPrompting="false" class="java.lang.String"> <defaultValueExpression ><![CDATA["AddressBookEMail.jasper"]]></defaultValueExpression> </parameter> <field name="CATEGORY" class="java.lang.String"> <fieldDescription><![CDATA[../@name]]></fieldDescription> </field> <field name="PERSON_ID" class="java.lang.String"> <fieldDescription><![CDATA[@id]]></fieldDescription> </field> <field name="LASTNAME" class="java.lang.String"> <fieldDescription><![CDATA[LASTNAME]]></fieldDescription> </field> <field name="FIRSTNAME" class="java.lang.String"> <fieldDescription><![CDATA[FIRSTNAME]]></fieldDescription> </field> <field name="EMAILADDRESS" class="java.lang.Object"> <fieldDescription><![CDATA[/addressbook/category/person/email]]></fieldDescription> </field> <group name="CATEGORY" > <groupExpression><![CDATA[$F{CATEGORY}]]></groupExpression> <groupHeader> <band height="25" isSplitAllowed="true" > <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" > <reportElement mode="Transparent" x="4" y="0" width="217" height="20" forecolor="#990033" backcolor="#FFFFFF" key="textField-1" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/> <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single"> <font fontName="Arial" pdfFontName="Helvetica" size="12" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{CATEGORY}]]></textFieldExpression> </textField> <line direction="TopDown"> <reportElement mode="Opaque" x="1" y="20" width="500" height="0" forecolor="#000000" backcolor="#FFFFFF" key="line-1" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <graphicElement stretchType="NoStretch" pen="Thin" fill="Solid" /> </line> </band> </groupHeader> <groupFooter> <band height="30" isSplitAllowed="true" > </band> </groupFooter> </group> <background> <band height="0" isSplitAllowed="true" > </band> </background> <title> <band height="50" isSplitAllowed="true" > <staticText> <reportElement mode="Transparent" x="219" y="2" width="109" height="29" forecolor="#000000" backcolor="#FFFFFF" key="staticText-2" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/> <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single"> <font fontName="Arial" pdfFontName="Helvetica" size="14" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <text><![CDATA[Address Book]]></text> </staticText> </band> </title> <pageHeader> <band height="0" isSplitAllowed="true" > </band> </pageHeader> <columnHeader> <band height="0" isSplitAllowed="true" > </band> </columnHeader> <detail> <band height="66" isSplitAllowed="true" > <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" > <reportElement mode="Transparent" x="25" y="1" width="321" height="19" forecolor="#000000" backcolor="#FFFFFF" key="textField-3" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/> <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single"> <font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{LASTNAME}+ " , "+$F{FIRSTNAME}]]></textFieldExpression> </textField> <line direction="BottomUp"> <reportElement mode="Opaque" x="40" y="35" width="460" height="2" forecolor="#000000" backcolor="#FFFFFF" key="line-2" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <graphicElement stretchType="NoStretch" pen="Thin" fill="Solid" /> </line> <staticText> <reportElement mode="Opaque" x="40" y="20" width="44" height="16" forecolor="#FF00FF" backcolor="#FFFFFF" key="staticText-4" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/> <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single"> <font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <text><![CDATA[E-Mail]]></text> </staticText> <subreport isUsingCache="true"> <reportElement mode="Opaque" x="40" y="42" width="455" height="20" forecolor="#000000" backcolor="#FFFFFF" key="subreport-1" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("/person/email")]]></dataSourceExpression> <subreportExpression class="java.lang.String"><![CDATA[$P{EMailSubReport}]]></subreportExpression> </subreport> </band> </detail> <columnFooter> <band height="0" isSplitAllowed="true" > </band> </columnFooter> <pageFooter> <band height="0" isSplitAllowed="true" > </band> </pageFooter> <summary> <band height="40" isSplitAllowed="true" > <staticText> <reportElement mode="Transparent" x="224" y="1" width="88" height="20" forecolor="#000000" backcolor="#FFFFFF" key="staticText-3" stretchType="NoStretch" positionType="FixRelativeToBottom" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/> <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single"> <font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <text><![CDATA[End of Report]]></text> </staticText> </band> </summary> </jasperReport> Sub-report for e-mail addresses <?xml version="1.0" encoding="UTF-8" ?> <!-- Created with iReport - A designer for JasperReports --> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="AddressBookEMail" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="565" pageHeight="842" columnWidth="565" columnSpacing="0" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" whenNoDataType="NoPages" isTitleNewPage="false" isSummaryNewPage="false"> <property name="ireport.scriptlethandling" value="0" /> <property name="ireport.encoding" value="UTF-8" /> <import value="java.util.*" /> <import value="net.sf.jasperreports.engine.*" /> <import value="net.sf.jasperreports.engine.data.*" /> <field name="EMAIL" class="java.lang.String"> <fieldDescription><![CDATA[.]]></fieldDescription> </field> <background> <band height="0" isSplitAllowed="true" > </band> </background> <title> <band height="0" isSplitAllowed="true" > </band> </title> <pageHeader> <band height="0" isSplitAllowed="true" > </band> </pageHeader> <columnHeader> <band height="0" isSplitAllowed="true" > </band> </columnHeader> <detail> <band height="19" isSplitAllowed="true" > <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" > <reportElement mode="Transparent" x="16" y="1" width="300" height="18" forecolor="#000000" backcolor="#FFFFFF" key="textField-1" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/> <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single"> <font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{EMAIL}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band height="0" isSplitAllowed="true" > </band> </columnFooter> <pageFooter> <band height="0" isSplitAllowed="true" > </band> </pageFooter> <summary> <band height="0" isSplitAllowed="true" > </band> </summary> </jasperReport> The data file (I've added some extra e-mail addresses) <addressbook> <category name="home"> <person id="1"> <LASTNAME>Davolio</LASTNAME> <FIRSTNAME>Nancy</FIRSTNAME> <hobbies> <hobby>Radio Control</hobby> <hobby>R/C Cars</hobby> <hobby>Micro R/C Cars</hobby> <hobby>Die-Cast Models</hobby> </hobbies> <email>email1@my.domain.it</email> <email>awemail@my.domain2.it</email> </person> <person id="2"> <LASTNAME>Fuller</LASTNAME> <FIRSTNAME>Andrew</FIRSTNAME> <email>email3@my.domain3.it</email> <email>email4@my.domain4.it</email> </person> <person id="3"> <LASTNAME>Leverling</LASTNAME> <FIRSTNAME>Janet</FIRSTNAME> <hobbies> <hobby>Rockets</hobby> <hobby>Puzzles</hobby> <hobby>Science Hobby</hobby> <hobby>Toy Horse</hobby> </hobbies> <email>email45@my.domain3.it</email> <email>email455@my.domain4.it</email> </person> </category> <category name="work"> <person id="4"> <LASTNAME>Peacock</LASTNAME> <FIRSTNAME>Margaret</FIRSTNAME> <hobbies> <hobby>Toy Horse</hobby> </hobbies> <email>Peacock@margaret.com</email> </person> <person id="5"> <LASTNAME>Buchanan</LASTNAME> <FIRSTNAME>Steven</FIRSTNAME> <hobbies> </hobbies> <email>Buchanan@steven.com</email> </person> <person id="6"> <LASTNAME>Suyama</LASTNAME> <FIRSTNAME>Michael</FIRSTNAME> </person> <person id="7"> <LASTNAME>King</LASTNAME> <FIRSTNAME>Robert</FIRSTNAME> </person> </category> <category name="Other"> <person id="8"> <LASTNAME>Callahan</LASTNAME> <FIRSTNAME>Laura</FIRSTNAME> <email>awemail5@my.domain3.it</email> </person> <person id="9"> <LASTNAME>Dodsworth</LASTNAME> <email>Dodsworth@my.anne.it</email> </person> </category> </addressbook> And finally here's the noddy which finds and display everything correctly. import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRField; import net.sf.jasperreports.engine.data.JRXmlDataSource; import net.sf.jasperreports.engine.design.JRDesignField; public class AWXML1 { public static void main(String[] args) throws Exception { JRXmlDataSource ds = new JRXmlDataSource(new FileInputStream("C:/jasperreports/jasperreports-1.1.1/demo/samples/addressbook/addressbook.xml"),"/addressbook/category/person"); JRDesignField field = new JRDesignField(); field.setDescription("LASTNAME"); field.setValueClass(java.lang.String.class); while (ds.next()) { String v = (String) ds.getFieldValue(field); System.out.println("main report :"+field.getDescription() + "=" + v); JRXmlDataSource subds = ds.subDataSource("/person/email"); JRDesignField field1 = new JRDesignField(); field1.setDescription("."); field1.setValueClass(java.lang.String.class); while (subds.next()) { String v1 = (String) subds.getFieldValue(field1); System.out.println("subreport :"+field1.getDescription() + "=" + v1); } } } } What am I doing wrong? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now