I'm wanting to use a args string that I'm passing into Java as a data source to fill my report. I have been attempting to create a JRXmlDataSource from the string to pass into fillreport. I've tried converting the string to a Doc, convert the string into a filestream, and just loading the string directly. I have even attempted to just create a file and load it in that way but nothing works. Any help would be appreciated.
Main Method:
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;
}
}
Source String:
<?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>
Jasper XML---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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…