有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自字符串的java数据源

我想使用传递到Java中的args字符串作为数据源来填充报告。我一直在尝试从字符串创建一个JRXmlDataSource以传递到fillreport。我尝试将字符串转换为文档,将字符串转换为文件流,然后直接加载字符串。我甚至尝试过创建一个文件并以这种方式加载它,但没有任何效果。任何帮助都将不胜感激

主要方法:

public class Testing {

    public static void main(String[] args) throws JRException {
        try {
            String xmlData = args[0];
            JasperReport jasperReport = null;
            String path = "C:/workspace/testProject/testing/sources/";
            JasperPrint jasperPrint = null;

            Document doc = convertStringToDocument(xmlData);
            JRXmlDataSource xmlDataSource = new JRXmlDataSource(doc);

            //Provide path for your JRXML template.
            String templateName = path + "report2.jrxml";

            //Provide path for your final pdf file.
            String destinationFile = path + "ReportSQL.pdf";

            //Compiling the template.
            jasperReport = JasperCompileManager.compileReport(templateName);

            //Sending a parameter with the logged in user name as value
            HashMap parameters = new HashMap();

            // Filling the report template with data
            jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, xmlDataSource);

            //Sending a Complete print of the report.
            JasperPrintManager.printReport(jasperPrint, true);

            //Exporting it to an PDF
            JasperExportManager.exportReportToPdfFile(jasperPrint, destinationFile);

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private static Document convertStringToDocument(String xmlStr) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

源字符串:

 <?xml version="1.0" encoding="UTF-8"?>
 <DATA>
  <ROW>
    <ID>1</ID>
    <firstName>Testname</firstName>
    <lastName>Testlast</lastName>
    <code>12345</code>
 </ROW>
 <ROW>
    <ID>2</ID>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
    <code>54321</code>
 </ROW>
 <ROW>
    <ID>3</ID>
    <firstName>Jane</firstName>
    <lastName>Doe</lastName>
    <code>22222</code>
</ROW>
<ROW>
    <ID>4</ID>
    <firstName>George</firstName>
    <lastName>Washington</lastName>
    <code>33333</code>
</ROW>
<ROW>
    <ID>5</ID>
    <firstName>John</firstName>
    <lastName>Adams</lastName>
    <code>44444</code>
</ROW>
<ROW>
    <ID>6</ID>
    <firstName>Thomas </firstName>
    <lastName>Jefferson</lastName>
    <code>55555</code>
</ROW>
<ROW>
    <ID>7</ID>
    <firstName>James</firstName>
    <lastName>Madison</lastName>
    <code>66666</code>
</ROW>
<ROW>
    <ID>8</ID>
    <firstName>James</firstName>
    <lastName>Monroe</lastName>
    <code>77777</code>
</ROW>
<ROW>
    <ID>9</ID>
    <firstName>John Quency</firstName>
    <lastName>Adams</lastName>
    <code>88888</code>
 </ROW>
 <ROW>
    <ID>10</ID>
    <firstName>Andrew</firstName>
    <lastName>Jackson</lastName>
    <code>99999</code>
 </ROW>
 <ROW>
    <ID>11</ID>
    <firstName>Martin Van</firstName>
    <lastName>Burren</lastName>
    <code>0</code>
 </ROW>
  </DATA>

JasperXML---report2。jrxml:

     <?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5263a626-fdd2-43a7-b67e-ced19b4c240b">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <field name="firstName" class="java.lang.String">
            <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <field name="lastName" class="java.lang.String">
              <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <field name="code" class="java.lang.Integer">
             <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <variable name="testout" class="java.lang.Integer">
              <variableExpression><![CDATA[$V{testout}.valueOf($V{testout}.intValue() + 1 )]]></variableExpression>
              <initialValueExpression><![CDATA[$V{testout}.intValue(0)]]></initialValueExpression>
        </variable>
        <variable name="variable1" class="java.lang.String"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
          <band height="71" splitType="Stretch">
           <staticText>
             <reportElement mode="Opaque" x="0" y="2" width="555" height="55" forecolor="#000000" backcolor="#6699FF" uuid="d2f1857a-a53f-4b65-8960-ff56856000c4"/>
               <textElement textAlignment="Center">
                   <font fontName="SansSerif" size="40"/>
               </textElement>
                <text><![CDATA[People Test]]></text>
             </staticText>
          </band>
      </title>
    <pageHeader>
        <band height="16" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="33" splitType="Stretch">
            <elementGroup>
                <staticText>
                    <reportElement mode="Opaque" x="360" y="0" width="195" height="20" backcolor="#999999" uuid="2311af72-d724-4003-b5e4-151bc23e41a8"/>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Code]]></text>
                 </staticText>
                 <staticText>
                    <reportElement mode="Opaque" x="168" y="0" width="192" height="20" backcolor="#999999" uuid="0fc99eec-a7c3-468f-ba64-30f3140131b2"/>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Last]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="0" y="0" width="168" height="20" backcolor="#999999" uuid="b0a4ccab-fff0-4a77-ad52-1e4132a627e2"/>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[First ]]></text>
                </staticText>
            </elementGroup>
         </band>
     </columnHeader>
     <detail>
         <band height="36" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="168" height="20" uuid="ca9be109-f4d6-48d6-988f-a5d78352e578"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{firstName}]]></textFieldExpression>
             </textField>
            <textField>
                <reportElement x="168" y="0" width="192" height="20" uuid="1255a5e6-59b1-4d49-92e0-c6b64a0cbf93"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="360" y="0" width="195" height="20" uuid="1099096e-17b7-480c-8e9c-ffb50669326a"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
            </textField>
         </band>
     </detail>
     <columnFooter>
        <band height="28" splitType="Stretch"/>
     </columnFooter>
     <pageFooter>
         <band height="214" splitType="Stretch">
            <image>
                <reportElement mode="Opaque" x="194" y="0" width="166" height="178" backcolor="#000000" uuid="4299a501-2792-4c76-bc6d-506a480fa1a6"/>
                <imageExpression><![CDATA["C:\\workspace\\testProject\\testing\\sources\\Turkish_Van_Cat.jpg"]]></imageExpression>
            </image>
             <textField>
                <reportElement x="197" y="198" width="163" height="16" uuid="801deb03-5452-4bc6-935e-2c15a8977721"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
     </pageFooter>
     <summary>
        <band height="104" splitType="Stretch"/>
    </summary>
 </jasperReport>

共 (1) 个答案

  1. # 1 楼答案

    field元素不包含XML路径表达式。试试这个:

    <field name="firstName" class="java.lang.String">
        <fieldDescription><![CDATA[DATA/ROW/firstName]]></fieldDescription>
     </field>
     <field name="lastName" class="java.lang.String">
       <fieldDescription><![CDATA[DATA/ROW/lastName]]></fieldDescription>
     </field>
     <field name="code" class="java.lang.Integer">
       <fieldDescription><![CDATA[DATA/ROW/code]]></fieldDescription>
     </field>
    

    此外,我有时也会使用一些小技巧。在JRXmlDataSource周围创建一个包装类,在Jasper检索字段时转储字段内容:

    package <your package here>;
    import net.sf.jasperreports.engine.JRException;
    import net.sf.jasperreports.engine.JRField;
    import net.sf.jasperreports.engine.data.JRXmlDataSource;
    
    public class MyXmlDataSource extends JRXmlDataSource {
    
        public MyXmlDataSource(String x, String y) throws JRException
        {
            super(x,y);
        }
    
        public Object getFieldValue(JRField jrField) throws JRException {
            Object ret = super.getFieldValue(jrField);
            System.out.println(ret);
            return ret;
        }
    }
    

    然后,不是实例化JRXmlDataSource,而是实例化MyXmlDataSource。这将让您知道Jasper正在从XML中检索哪些元素