3 Replies Latest reply on Mar 11, 2009 11:41 AM by Patrick Pussar

    Smooks DB->XML

    Patrick Pussar Newbie

      Hi Forum,
      I try to write data from a DB to XML. Unfortunatley the documentation about enrichment is not very detailed.
      I have a xml doc with that structure:

       <dossier id="1" />
       <dossier id="2" />

      and a table with products:

      dossierId, productNo, productName,...

      goal is this:
       <dossier id="1">
       <product no="123" name="onetwothree"/>
       <dossier id="2">
       <product no="456" name="xy"/>
       <product no="789" name="xx"/>

      I want to hold (if possible) only one product row in memory, due to the product table is very big.
      I guess I need to use the resultSetRowSelector for this, but I found really no documentation about that.
      Can anyone provide me some documentation or even an example. I would really appreciate!!!

        • 1. Re: Smooks DB->XML
          Tom Fennelly Master

          Have you seen the quickstart: huge-split-enrich-transform-route?

          So firstly what you need to do is define the datasource, which would look something like:

          <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
           Configure the "OrderManagement" DB access datasource...
           <ds:direct bindOnElement="$document" datasource="productDatasource" autoCommit="false"
           driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:hsql://localhost:9002"
           username="sa" password="" />

          You'll need to select the id values from each dossier message fragment:

          <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
           <jb:bindings beanId="dossier" class="java.util.HashMap" createOnElement="dossier">
           <jb:value property="id" data="dossier/@id" decoder="Integer" />

          (Obviously you can merge the configs into a single smooks config).

          Next, you'll need to select the product record from the DB. You can select all products into a single resultset (you can also scope that around the "APPLICATION" so as not to be reading it every time) and then use row selector to select the product. For this, lets just read the specific product record for every dossier:

          <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
           <db:executor executeOnElement="dossier" datasource="productDatasource">
           <db:statement>select productNo, productName from PRODUCTS where dossierId = ${dossier.id}</db:statement>
           <db:resultSet name="product" />

          (Note how we use the dossier jb:binding data in the query)

          And then we need to apply a template to each dossier fragment to add the product info:

          <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
           <ftl:freemarker applyOnElement="dossier">
           <ftl:template><!--<product no="${product[0].productNo}" name="${product[0].productName}"/>--></ftl:template>
           <ftl:inline directive="addto" />

          Note how we have to reference the zero element in the product result set. This is because we don't use the row selector.

          Again, all these configs can go into a single Smooks config.

          • 2. Re: Smooks DB->XML
            Tom Fennelly Master

            I forgot to mention.... I didn't try this out... it's totally manufactured so you might have to play with it a bit :)

            • 3. Re: Smooks DB->XML
              Patrick Pussar Newbie

              Hi Tom,
              great! the inline directive was the missing link. The transformation works now fine :-)

              Thanks for the help!!!