1 Reply Latest reply on Jul 4, 2011 10:06 AM by mlog

    Java-to-xml transformation

    mlog Newbie

      Hi.

      I am trying to transform a java class to an xml document with Smooks and Freemarker template. I looked for any example, and found only one about java-to-xml default transformation and a lot about xml-to-anything with templating. But no one java-to-xml with templating. Then I played with example of default transformation and found out that I have to use fully qualified name of my class as Smooks resource selector and element name. Is it wrong? But now, I couldn’t use it in Freemarker template because the dots have another meaning inside “${}” construction.

      So the question is – how to use java-to-xml Smooks transformation with Freemarker template?

       

      This is my smooks-config.xml file. It works with static template (without “${}”). It is not useful, though.

       

      <smooks-resource-list
         xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
         xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
      >
       
         <params>
            <param name="stream.filter.type">SAX</param>
            <param name="default.serialization.on">false</param>
         </params>
      
         <resource-config selector="fully.qulified.object.name">
            <resource>org.milyn.delivery.DomModelCreator</resource>
         </resource-config>
      
         <ftl:freemarker applyOnElement="fully.qulified.object.name">
            <ftl:template><!--<object field=”${name.field}”/>--></ftl:template>
         </ftl:freemarker>
       
      </smooks-resource-list>

       

      Thanks in advance.

        • 1. Re: Java-to-xml transformation
          mlog Newbie

          I’m afraid that I didn’t clearly explain my problem. Ok then, I’ll try again.

          I have an action that produces a domain specific java class. I need to transform it to an xml document. I am trying to do that with Smooks. My class looks like this:

           

          package my.namespace.domain;
          
          class className {
                private String field;
                private anotherClassName innerClass;
          }
          

           

          The default Smooks transformation (just like java-to-xml Smooks’ example) produces this result:

           

          <my.namespace.domain.className>
                <field>field value</field>
                <innerClass>
                      <!-- inner class fields -->
                </innerClass>
          </my.namespace.domain.className>
          

           

          So the name of the root element is fully qualified java class name. Now, I can use Freemarker templates like ${innerClass.field} but cannot like ${my.namespace.domain.className.field}. That is I cannot use direct descendants of the root element in my template.

          Yes, I could wrap my class to another one (and I did for a while) but it isn’t the right way, is it?