3 Replies Latest reply on Mar 25, 2008 4:22 AM by Morten Hauch

    Smooks xml to Java

    Morten Hauch Newbie

      Hi,

      I have this simple setup where I would like to have the snippet below converted to pojos.

      <items>
       <subitem subitem_id="1">
       <child type="abc" val="27"/>
       <child type="def"/>
       </subitem>
       <subitem subitem_id="11">
       <child type="abc" val="5"/>
       </subitem>
      </items>
      

      I would like a List of SubItems, each looking somewhat like.
      class SubItem {
       List<Child> children;
      }
      

      Where the List consists of pojos corresponding to the childelements.

      But if I do something like...
      <?xml version='1.0' encoding='UTF-8'?>
      <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">
      
       <resource-config selector="items subitem">
       <resource>org.milyn.javabean.BeanPopulator</resource>
       <param name="beanId">subitem</param>
       <param name="beanClass">SubItem</param>
       <param name="addToList">true</param>
       <param name="bindings">
       <binding property="id" selector="items subitem @subitem_id"/>
       </param>
       </resource-config>
      
       <resource-config selector="items subitem child">
       <resource>org.milyn.javabean.BeanPopulator</resource>
       <param name="beanId">child</param>
       <param name="beanClass">Child</param>
       <param name="addToList">true</param>
       <param name="setOn">subitem</param>
       <param name="bindings">
       <binding property="type" selector="child @type" />
       <binding property="value" selector="child @val" />
       </param>
       </resource-config>
      
      </smooks-resource-list>
      

      ... all child elements are added to all subitems.

      So my question is: How I can make my smooks file, so the child elements are added as classes to (and only to) the class correspondig to the parent element?

      Any help is appreciated.


        • 1. Re: Smooks xml to Java
          Tom Fennelly Master

          This has been cleaned up and improved in Smooks v1.0, so I'll have to look back and see how this is done with v0.9 (used by the ESB).

          Will post back later.

          • 2. Re: Smooks xml to Java
            Tom Fennelly Master

            Had a look at this and it's a bug in Smooks v0.9 (fixed in v1.0). It's to do with how it manages the lifecycle on lists.

            The non-patch fix is straightforward enough and is available in the zip linked to below. Basically it's a new simple visitor that resets the bean list at the right time, resulting in a new "inner" list being added to each of the "outer" lists (as opposed to the same one being added to each).

            Download the following zip: beanpop2.zip. See the "RestBean" class in the src and how it's used in the smooks-config.xml.

            • 3. Re: Smooks xml to Java
              Morten Hauch Newbie

              I have tried it out, and it works fine.
              Thanks a lot!

              Is there a place, where it is described what it takes to perform the patch?