When using this kind of data source we can load the data from a resource resolvable through the built-in resource loading mechanism or by means of a backing bean which will offer the data to the data source component.
The attributes required to be used with this type of data soruce are reflected in the following table:
Attribute | Optional | Description |
value | false | XML Document which contains the data. |
query | true | XPath query used to obtain the result set. |
Valid values for attribute value are:
The most simple usage is based on the built-in resource loading mechanism implemented inside the plugin using following way:
<jr:source type="xml" value="classpath:META-INF/datasource/test.xml" />
First of all, we need to design a JSF Managed Bean which will build the XML document that will be used by the data source component.
import java.io.*; import javax.annotation.PreDestroy; import org.w3c.dom.Document; import ...; public class XMLDataBean { private InputStream xmlStream = null; ... // Dynamically created XML document public Document getXmlDocument() { // Build an XML Document } public InputStream getXmlStream() { if (xmlStream == null) { // Instantiate an input stream from a xml source } return xmlStream; } @PreDestroy public void release() { if (xmlStream != null) { try { xmlStream.close(); } catch(IOException e) { e.printStackTrace(); } } // Release other resources } ... }
Then, this managed bean can be used inside the JSP file that will render the report or report link.
<jr:source type="xml" value="#{XMLDataBean.xmlDocument}" />
<jr:source type="xml" value="#{XMLDataBean.xmlStream}" />
The same way you can use queries with JDBC or JNDI data sources, you can also use queries with XML data sources. In this case, the difference is the kind of query to be used, since we are dealing with XML, the query should be an XPath query.
<jr:source type="xml" value="#{XMLDataBean.xmlDocument}" query="SALES/ORDER[@city='{0}']" > <f:param value="Melbourne" /> </jr:source>
As you can see, you can parameterize queries but following the java message formatting syntax.