我遇到了一个真正需要帮助的问题。我负责创建一个应用程序。该应用程序有一些要求如下:
将它们保存为spec文档。 (见图)
这里的问题是如何动态地读取具有其属性的对象;因为不同的报告(* .jrxml)具有不同的xml格式。有任何想法吗?请帮忙......
以下是我已经尝试过的事情:
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="number-to-words" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20"
rightMargin="20" topMargin="20" bottomMargin="20" uuid="e37ef9d7-03e0-4cf0-aa3e-ddc6a4434211">
<property name="ireport.zoom" value="0.9090909090909091"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[select freight from orders]]>
</queryString>
<field name="freight" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="47" splitType="Stretch">
<staticText>
<reportElement uuid="cb175252-6939-4c44-972a-424c7cd71957" x="235" y="27" width="100" height="20"/>
<textElement textAlignment="Center">
<font size="14"/>
</textElement>
<text><![CDATA[Static text]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="21" splitType="Stretch">
<staticText>
<reportElement uuid="67d8b9b6-5f01-41ac-9a2a-ced60ebcae41" x="0" y="0" width="85" height="21"/>
<box leftPadding="4">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Number]]></text>
</staticText>
<staticText>
<reportElement uuid="82d78d65-6828-4a64-b3b3-69e5a4aea356" x="85" y="0" width="470" height="21"/>
<box leftPadding="4">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[As words]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="21" splitType="Stretch">
<textField>
<reportElement uuid="ee38dfd7-d417-4fe6-bb99-130c05074fca" x="0" y="0" width="85" height="21"/>
<box topPadding="0" leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA['$' + ($F{freight}+1000).toString()]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="b78301fb-10d4-4bd6-becb-9223c7de16f6" x="85" y="0" width="470" height="21"/>
<box topPadding="0" leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[new src.com.NumberToWord().ConvertNumber($F{freight}+1000)+ " Dollars"]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Java代码:
try{
File xmlFile = new File("jrxml.jrxml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(xmlFile);
document.getDocumentElement().normalize();
Element element = (Element) document.getDocumentElement();
NodeList nodeList = element.getChildNodes();
for(int i=0; i<nodeList.getLength(); i++){
Node node = nodeList.item(i);
if(node instanceof Element){
Element child = (Element) node;
String tagName = child.getTagName();
System.out.println(tagName);
}
}
}
catch(Exception e){
e.printStackTrace();
}
结果:它读了什么。
property
property
property
queryString
field
background
title
pageHeader
columnHeader
detail
columnFooter
pageFooter
summary