Class JRXmlDataSource

  • All Implemented Interfaces:
    HierarchicalDataSource<JRXmlDataSource>, RandomAccessDataSource, JRDataSource, JRRewindableDataSource

    public class JRXmlDataSource
    extends AbstractXmlDataSource<JRXmlDataSource>
    XML data source implementation that allows to access the data from a xml document using XPath expressions.

    The data source is constructed around a node set (record set) selected by an XPath expression from the XML document.

    Each field can provide an additional XPath expression that will be used to select its value. This expression must be specified using the AbstractXmlDataSource.PROPERTY_FIELD_EXPRESSION custom property at field level. The use of the field description to specify the XPath expression is still supported, but is now discouraged, the above mentioned custom property taking precedence over the field description. The expression is evaluated in the context of the current node thus the expression should be relative to the current node.

    To support subreports, sub data sources can be created. There are two different methods for creating sub data sources. The first one allows to create a sub data source rooted at the current node. The current node can be seen as a new document around which the sub data source is created. The second method allows to create a sub data source that is rooted at the same document that is used by the data source but uses a different XPath select expression.

    Example:

     <A>
            <B id="0">
                    <C>
                    <C>
            </B>
            <B id="1">
                    <C>
                    <C>
            </B>
            <D id="3">
                    <E>
                    <E>
            </D>
     </A>
     

    Data source creation

    • new JRXmlDataSource(document, "/A/B") - creates a data source with two nodes of type /A/B
    • new JRXmlDataSource(document, "/A/D") - creates a data source with two nodes of type /A/D
    Field selection
    • @id - will select the "id" attribute from the current node
    • C - will select the value of the first node of type C under the current node.
    Sub data source creation
    • "((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/B/C") - in the context of the node B, creates a data source with elements of type /B/C
    • "((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/A/D") - creates a data source with elements of type /A/D

    Generally the full power of XPath expression is available. As an example, "/A/B[@id > 0"] will select all the nodes of type /A/B having the id greater than 0. You'll find a short XPath tutorial here.

    Note on performance. Due to the fact that all the XPath expression are interpreted the data source performance is not great. For the cases where more speed is required, consider implementing a custom data source that directly accesses the Document through the DOM API.

    Author:
    Peter Severin (peter_p_s@sourceforge.net, contact@jasperassistant.com)
    See Also:
    JRXPathExecuterUtils