xpathjasper-reportsxmldatasource

How to print nested xml elements?


Sample xml:

<Root>
  <Customers>
    <Customer>
      <CompanyName>Great Lakes Food Market</CompanyName>
      <ContactName>Howard Snyder</ContactName>
      <ContactTitle>Marketing Manager</ContactTitle>
      <Phone>(503) 555-7555</Phone>
      <FullAddress>
        <Address>2732 Baker Blvd.</Address>
        <City>Eugene</City>
        <Region>OR</Region>
        <PostalCode>97403</PostalCode>
        <Country>USA</Country>
      </FullAddress>
    </Customer>
  </Customers>
</Root>

In the above xml, when I use "Customer" as the root node and xpath query as "/Root/Customers/Customer", I'm unable to print the child nodes of "FullAddress" and when I use "FullAddress" as the root node and the xpath query as "/Root/Customers/Customer/FullAddress", unable to print all the fields.

Kindly help me with the solution to print all the xml elements including the nested in a single report.


Solution

  • The correct XPath query is

    <queryString language="XPath">
        <![CDATA[/Root/Customers/Customer]]>
    </queryString>
    

    This include both of your nodes, to access the value is FullAddress node you should use XPath also in fieldDescription when you define your field, hence Address is accessed through FullAddress/Address

    Example

    If the field declaration of CompanyName is

    <field name="CompanyName" class="java.lang.String">
        <fieldDescription><![CDATA[CompanyName]]></fieldDescription>
    </field>
    

    the field declaration of for example the City is

    <field name="City" class="java.lang.String">
        <fieldDescription><![CDATA[FullAddress/City]]></fieldDescription>
    </field>