使用XML数据源的Jasper报告不显示表行

时间:2013-08-19 11:00:58

标签: java xml jasper-reports datasource xmltable

我有以下XML数据源 - 文件:

<soapenv:Envelope
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:typ="http://mytypes.cz/types/">
    <soapenv:Header/>
    <soapenv:Body>
        <typ:topElement>
            <typ:elementHeader>
                <typ:createDate>2013-07-24T11:27:29.8751673+02:00</typ:createDate>
            </typ:elementHeader>
            <typ:mainElement>
                <typ:myElementNumber>123</typ:myElementNumber>
                <typ:myElementDate>2013-07-24T11:27:29.8751673+02:00</typ:myElementDate>
                <typ:myElementString>some text</typ:myElementString>
                <typ:listItems>
                    <typ:itemElement>
                        <typ:fieldText>some text 1</typ:fieldText>
                        <typ:fieldNumber>1</typ:fieldNumber>
                    </typ:itemElement>
                    <typ:itemElement>
                        <typ:fieldText>some text 2</typ:fieldText>
                        <typ:fieldNumber>2</typ:fieldNumber>
                    </typ:itemElement>
                </typ:listItems>
            </typ:mainElement>
        </typ:topElement>
    </soapenv:Body>
</soapenv:Envelope>

我的 JRXML 就在这里:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version last -->
<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="sample_report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"
              topMargin="20" bottomMargin="20" uuid="b56a28bb-02f7-4e2c-9aec-5e6b2c2ba863">
    <queryString language="XPath">
        <![CDATA[/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='topElement']/*[local-name()='mainElement']/*[local-name()='listItems']/*[local-name()='itemElement']]]>
    </queryString>
    <field name="typ:fieldText" class="java.lang.String">
        <fieldDescription><![CDATA[*[local-name()='fieldText']]]></fieldDescription>
    </field>
    <field name="typ:fieldNumber" class="java.lang.String">
        <fieldDescription><![CDATA[*[local-name()='fieldNumber']]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <textField>
                <reportElement uuid="c117b351-e8b4-48c7-b26e-2a8b79641d2d" x="27" y="10" width="70" height="30"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{typ:fieldText}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="fc7c6016-6294-41a5-8414-1ba7109030ef" x="110" y="10" width="70" height="30"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{typ:fieldNumber}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

和Java来源:

@Test
public void testProcessAdmOzsUhrada1() throws JRException {
    try {
        JasperReportsProcessor processor = new JasperReportsProcessor();
        InputStream is = this.getClass().getClassLoader()
                .getResourceAsStream("sample_report.jasper");
        System.out.println("japser precten");
        File fXmlFile = new File("./src/test/resources/sample_data.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        JRXmlDataSource source = new JRXmlDataSource(doc);


        Map<String, Object> params = new HashMap<String, Object>();
        byte[] bArray = processor.processToPdf(is, params, source);
        OutputStream out = new FileOutputStream("d:/sample_report.pdf");
        out.write(bArray);
        out.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

当我使用DS从Jasper Studio运行此报告时:

<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample Data Adapter"/>

工作正常。但是当我从Java运行它时,我只有一行(null,null),没有更多。构造/*[local-name()='field']是必要的,以避免名称空间问题。

有人知道如何修理它吗?

0 个答案:

没有答案