Jump to content
JasperReports Library 7.0 is now available ×

XMLDatasource & XPath


julay

Recommended Posts

Hi!

 

I'm trying to use an XMLDatasource to retrieve the values from an xml file, and I have the values I expected mixed with a lot of null values, I have solve this problem using the 'print when expression' but this is not enough because I need only the values from the xml document to show a chart in my report, and with the null values this is not possible. I've tried also modifying the xml by adding some attributes in the fields I need and asking for this values, but it only works defining the query in the report query, not in the field definition.

 

The xml file follows

Code:

<?xml version="1.0" encoding="UTF-8"?>
<ResponseInfo>
<url id="1">
<name>http://www.google.com</name>
<time value="609">609</time>
<size size="218">218</size>
<statusCode>302</statusCode>
<status>MovedTemporaly</status>
<date>2006-09-06 00:25:23.6</date>
<url id="1.1">
<name>http://www.google.es/</name>
<time value="266">266</time>
<size size="0">0</size>
<statusCode>200</statusCode>
<status>Ok, Success</status>
<date>2006-09-06 00:45:27.113</date>
<images>
<image>
<name>http://www.google.es/images/image1-1-1.gif</name>
<time value="453">453</time>
<size size="44">44</size>
<status>200</status>
<date>2006-09-06 01:05:31.758</date>
</image>
<image>
<name>http://www.google.es/images/image1-1-2.gif</name>
<time value="453">453</time>
<size size="4277">4277</size>
<status>200</status>
<date>2006-09-06 01:25:35.747</date>
</image>
</images>
<url id="1.1.1">
<name>http://www.google.es/url?sa=p&pref=ig&pval=3&
q=http://www.google.es/ig%3Fhl%3Des&usg=__6aryJ-3r2l0I91WnpwNJ0zsplmE=</name>
<time>235</time>
<size>226</size>
<statusCode>302</statusCode>
<status>MovedTemporaly</status>
<date>2006-09-06 01:45:53.878</date>
<images>
<image>
<name>http://www.google.es/images/image1-1-1-1.gif</name>
<time>360</time>
<size>2953</size>
<status>200</status>
<date>2006-09-06 07:22:49.97</date>
</image>
<image>
<name>http://www.google.es/images/image1-1-1-2.gif</name>
<time>375</time>
<size>2702</size>
<status>200</status>
<date>2006-09-06 07:42:48.829</date>
</image>
</images>
<url id="1.1.1.1">
<name>http://www.google.es/ig?hl=es</name>
<time>328</time>
<size>39961</size>
<statusCode>200</statusCode>
<status>Ok, Success</status>
<date>2006-09-06 08:02:48.721</date>
<images>
<image>
<name>http://www.google.es/images/image1-1-1-1-1.gif</name>
<time>422</time>
<size>1410</size>
<status>200</status>
<date>2006-09-06 08:22:46.56</date>
</image>
<image>
<name>http://www.google.es/images/image1-1-1-1-2.gif</name>
<time>437</time>
<size>2702</size>
<status>200</status>
<date>2006-09-06 13:56:45.319</date>
</image>
</images>
</url>
</url>
<url id="1.1.2">
<name>https://www.google.com/accounts/Login?continue=http://www.google.es/&
hl=es</name>
<time>10500</time>
<size>0</size>
<statusCode>103</statusCode>
<status>TimeOut [Read timed out]</status>
<date>2006-09-06 14:07:34.047</date>
</url>
<url id="1.1.3">
<name>http://images.google.es/imghp?ie=ISO-8859-1&oe=ISO-8859-1&hl=es&
tab=wi</name>
<time>250</time>
<size>2603</size>
<statusCode>200</statusCode>
<status>Ok, Success</status>
<date>2006-09-06 14:09:19.414</date>
<images>
<image>
<name>http://www.google.es/images/hp1.gif</name>
<time>343</time>
<size>2953</size>
<status>200</status>
<date>2006-09-06 14:09:19.414</date>
</image>
</images>
<url id="1.1.3.1">
<name>
https://www.google.com/accounts/Login?continue=http://images.google.es/imghp%3Fie%3DISO-8859-1%26oe%3DISO-8859-1%26hl%3Des%26tab%3Dwi&
hl=es</name>
<time>10016</time>
<size>0</size>
<statusCode>103</statusCode>
<status>TimeOut [Read timed out]</status>
<date>2006-09-06 14:09:48.221</date>
</url>
<url id="1.1.3.2">
<name>http://www.google.es/webhp?ie=ISO-8859-1&oe=ISO-8859-1&hl=es&
tab=iw</name>
<time>250</time>
<size>3617</size>
<statusCode>200</statusCode>
<status>Ok, Success</status>
<date>2006-09-06 14:09:48.222</date>
<images>
<image>
<name>http://www.google.es/images/hp3.gif</name>
<time>390</time>
<size>1410</size>
<status>200</status>
<date>2006-09-06 14:09:48.223</date>
</image>
<image>
<name>http://www.google.es/images/hp1.gif</name>
<time>406</time>
<size>2953</size>
<status>200</status>
<date>2006-09-06 14:09:48.224</date>
</image>
<image>
<name>http://www.google.com/images/logo_sm.gif</name>
<time>437</time>
<size>4707</size>
<status>200</status>
<date>2006-09-06 14:09:48.225</date>
</image>
<image>
<name>http://www.google.es/images/hp0.gif</name>
<time>453</time>
<size>4277</size>
<status>200</status>
<date>2006-09-06 14:09:48.226</date>
</image>
<image>
<name>http://www.google.es/ig/images/yellow_warning.gif</name>
<time>297</time>
<size>886</size>
<status>200</status>
<date>2006-09-06 14:09:48.227</date>
</image>
<image>
<name>http://www.google.es/ig/images/x3.gif</name>
<time>313</time>
<size>123</size>
<status>200</status>
<date>2006-09-06 14:09:48.228</date>
</image>
<image>
<name>http://www.google.es/ig/modules/eyes_content/pupil.gif</name>
<time>297</time>
<size>568</size>
<status>200</status>
<date>2006-09-06 14:09:48.229</date>
</image>
<image>
<name>http://www.google.es/ig/images//weather_welcome_image.jpg</name>
<time>313</time>
<size>2260</size>
<status>200</status>
<date>2006-09-06 14:09:48.230</date>
</image>
<image>
<name>http://www.google.es/ig/modules/eyes_content/pupil.gif</name>
<time>328</time>
<size>568</size>
<status>200</status>
<date>2006-09-06 14:09:48.231</date>
</image>
<image>
<name>http://www.google.es/images/hp1.gif</name>
<time>312</time>
<size>2953</size>
<status>200</status>
<date>2006-09-06 14:09:48.232</date>
</image>
<image>
<name>http://images.google.es/intl/es_ALL/images/images_hp.gif</name>
<time>375</time>
<size>9754</size>
<status>200</status>
<date>2006-09-06 14:09:48.233</date>
</image>
<image>
<name>http://www.google.es/images/hp0.gif</name>
<time>375</time>
<size>4277</size>
<status>200</status>
<date>2006-09-06 14:09:48.234</date>
</image>
<image>
<name>http://www.google.es/images/hp3.gif</name>
<time>282</time>
<size>1410</size>
<status>200</status>
<date>2006-09-06 14:09:48.235</date>
</image>
<image>
<name>http://www.google.es/images/hp2.gif</name>
<time>329</time>
<size>2702</size>
<status>200</status>
<date>2006-09-06 14:09:48.236</date>
</image>
<image>
<name>http://www.google.es/images/hp2.gif</name>
<time>1406</time>
<size>2702</size>
<status>200</status>
<date>2006-09-06 14:09:48.237</date>
</image>
</images>
</url>
</url>
</url>
</url>
</ResponseInfo>

 

I've tried using this xpath expression as report query: //* and defining the fields with size/@size and time/@time and I have the same result (values mixed with a lot of null values).

 

The report source is this

Code:
[code]
<?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="XMLSubreportTime"
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.*" />

<queryString language="xPath"><![CDATA[//*]]></queryString>

<field name="size" class="java.lang.Integer">
<fieldDescription><![CDATA[size/@size]]></fieldDescription>
</field>
<field name="value" class="java.lang.Integer">
<fieldDescription><![CDATA[time/@value]]></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="21" isSplitAllowed="true" >
<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="2"
y="1"
width="100"
height="18"
key="textField"
isRemoveLineWhenBlank="true"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{size}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="108"
y="1"
width="100"
height="18"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="0" isSplitAllowed="true" >
</band>
</columnFooter>
<pageFooter>
<band height="0" isSplitAllowed="true" >
</band>
</pageFooter>
<lastPageFooter>
<band height="100" isSplitAllowed="true" >
</band>
</lastPageFooter>
<summary>
<band height="0" isSplitAllowed="true" >
</band>
</summary>
</jasperReport>

Can you help me?

 

Thank you!

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...