10 Replies Latest reply on Mar 20, 2011 11:48 PM by simon zhu

    How can i use smooks to transform a java object to a formed xml?

    simon zhu Newbie

      I had read a input xml message to a java object which the xml message like below

       

      <order>

          <header>

              <date>Wed Nov 15 13:45:28 EST 2006</date>

              <customer number="123123">Joe</customer>

          </header>

          <order-items>

              <order-item>

                  <product>111</product>

                  <quantity>2</quantity>

                  <price>8.90</price>

              </order-item>-->

              <order-item>

                  <product>222</product>

                  <quantity>7</quantity>

                  <price>5.20</price>

              </order-item>

          </order-items>

      </order>

       

      However,smooks sovled this at a good work.

      Then,I want to transform this java object to another formed xml message  like below

      <Product>
                <baseinfo>
                          <date>2006-11-16 02:45:28.0 CST</date>
                          <customer id="123123">Joe</customer>
                </baseinfo>
                <Items>
                          <Item>
                                    <productId>111</productId>
                                    <quantity>2</quantity>
                                    <price>8.9</price>
                          </Item>
                          <Item>
                                    <productId>222</productId>
                                    <quantity>7</quantity>
                                    <price>5.2</price>
                          </Item>
                </Items>
      </Product>
      

       

      How can i do this with smooks?    Very,very,very tks!

        • 1. Re: How can i use smooks to transform a java object to a formed xml?
          Daniel Bevenius Master

          Hi,

           

          I would use a FreeMarker template for that. There are several quickstarts that use this technique, for example the transform_XML2XML_date_manipulation quickstart.

           

          Smooks also ships with examples on how to do this. Take a look at the [FreeMarker Templating|http://www.smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#FreeMarker_Templating]  in the Smooks User Guide for more details.

           

          Regards,

           

          /Daniel

          • 2. How can i use smooks to transform a java object to a formed xml?
            simon zhu Newbie

            Thanks for your answer.

            I'd already look at the example you've mention,the transform XML2XML worked well but Java2XML not.

            • 3. How can i use smooks to transform a java object to a formed xml?
              simon zhu Newbie

              In this situation,

                 Smooks smooks = new Smooks();

              has the same result with

                 Smooks smooks = new Smooks("smooks-config.xml");

               

              Here is the result after the transformaton.

               

              <example.model.Order>

                        <header>

                                  <date>2006-11-16 02:45:28.0 CST</date>

                                  <customerNumber>123123</customerNumber>

                                  <customerName>Joe</customerName>

                        </header>

                        <orderItems>

                                  <example.model.OrderItem>

                                            <productId>111</productId>

                                            <quantity>2</quantity>

                                            <price>8.9</price>

                                  </example.model.OrderItem>

                                  <example.model.OrderItem>

                                            <productId>222</productId>

                                            <quantity>7</quantity>

                                            <price>5.2</price>

                                  </example.model.OrderItem>

                        </orderItems>

              </example.model.Order>

              • 4. Re: How can i use smooks to transform a java object to a formed xml?
                Daniel Bevenius Master

                In this situation,

                   Smooks smooks = new Smooks();

                has the same result with

                   Smooks smooks = new Smooks("smooks-config.xml");

                 

                I would guess that this is because there is no transformation is taking place, like if no selector matches then what you are seeing is the event stream that Smooks generates as it reads the input data stream which in your case is a Java Object.

                Take a look at the [Introduction | http://www.smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Introduction] section which explains this.

                 

                You might like to test you Smooks transformation in a unit test to make sure that it is working as expected before trying it in JBossESB. You can find an example of this in the [Unit Testing | http://www.smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Unit_Testing] section.

                 

                Regards,

                 

                /Daniel

                1 of 1 people found this helpful
                • 5. Re: How can i use smooks to transform a java object to a formed xml?
                  simon zhu Newbie

                  Well,the question had bean resolved.

                  thank you , Daniel.

                   

                  All the keywords are about this:

                  <ftl:freemarker applyOnElement="example.model.Order">

                  which i configured earlier is :

                  <ftl:freemarker applyOnElement="order">

                   

                  Now , the basic problem has been resolved. But there is a new problem .

                  How to configure smooks for the result like below?

                      <order-items>
                          <order-item>
                              <product>111</product>
                              <quantity>2</quantity>
                              <price>8.90</price>
                          </order-item>-->
                          <order-item>
                              <product>222</product>
                              <quantity>7</quantity>
                              <price>5.20</price>
                          </order-item>
                      </order-items>
                  

                   

                  BTW. the "example.model.Order" object has a java.util.List property named orderItems which generic type is example.model.OrderItem

                  • 6. Re: How can i use smooks to transform a java object to a formed xml?
                    simon zhu Newbie

                    My current smooks configuration is

                     

                     <core:filterSettings type="SAX" defaultSerialization="true" />
                    
                              <resource-config selector="order,example.model.Order.orderItems">
                            <resource>org.milyn.delivery.DomModelCreator</resource>
                        </resource-config>
                    
                              <ftl:freemarker applyOnElement="example.model.Order">
                                        <ftl:template>order.ftl</ftl:template>
                              </ftl:freemarker>
                    
                    
                              <ftl:freemarker applyOnElement="example.model.Order.orderItems">
                                        <ftl:template>orderItem.ftl</ftl:template>
                              </ftl:freemarker>
                    

                     

                    The content of "orderITem.ftl" is :

                     

                    <order-item>

                              <productId>${.vars["example.model.Order.orderItems"].productId}</productId>

                              <quantity>${.vars["example.model.Order.orderItems"].quantity}</quantity>

                              <price>${.vars["example.model.Order.orderItems"].price}</price>

                    </order-item>

                    • 7. Re: How can i use smooks to transform a java object to a formed xml?
                      simon zhu Newbie

                      The content of "order.ftl" is :

                      <order>
                                <header>
                                          <customer>
                                                    <id>${order.header.customerNumber}</id>
                                                    <name>${order.header.customerName}</name>
                                          </customer>
                                </header>
                                <order-Items>
                                          <?TEMPLATE-SPLIT-PI?>
                                </order-Items>
                      </order>
                      
                      • 8. Re: How can i use smooks to transform a java object to a formed xml?
                        simon zhu Newbie

                        If i configured the smook-config.xml and the .ftl file like below

                         

                        smook-config.xml:

                        <resource-config selector="order,orderItem">
                                <resource>org.milyn.delivery.DomModelCreator</resource>
                            </resource-config>
                        
                                  <ftl:freemarker applyOnElement="example.model.Order">
                                            <ftl:template>order.ftl</ftl:template>
                                  </ftl:freemarker>
                        
                        <ftl:freemarker applyOnElement="example.model.OrderItem">
                                            <ftl:template>orderItem.ftl</ftl:template>
                                  </ftl:freemarker>
                        
                        

                        orderItem.ftl:

                        <order-item>
                                  <productId>${.vars[""example.model.OrderItem""].productId}</productId>
                                  <quantity>${.vars[""example.model.OrderItem""].quantity}</quantity>
                                  <price>${.vars[""example.model.OrderItem""].price}</price>
                        </order-item>
                        

                         

                         

                        There would be an exception thrown:

                        freemarker.core.InvalidReferenceException: Expression .vars["example.model.OrderItem"] is undefined on line 2, column 22

                         

                        What  the vars should been configured?

                         

                        Best regards.

                        • 10. Re: How can i use smooks to transform a java object to a formed xml?
                          simon zhu Newbie

                          Allright,if the smooks configured like below,the transformation works well.

                           

                          <core:filterSettings type="SAX" defaultSerialization="false" />
                          
                                    <resource-config selector="example.model.Order,example.model.OrderItem">
                                  <resource>org.milyn.delivery.DomModelCreator</resource>
                              </resource-config>
                          
                                    <ftl:freemarker applyOnElement="example.model.Order">
                                              <ftl:template>
                                                        <!--<order>
                                                                  <header>
                                                                            <customer>
                                                                                      <id>${order.header.customerNumber}</id>
                                                                                      <name>${order.header.customerName}</name>
                                                                            </customer>
                                                                  </header>
                                                                  <order-Items>
                                                                            <?TEMPLATE-SPLIT-PI?>
                                                                  </order-Items>
                                                        </order>
                                              --></ftl:template>
                                    </ftl:freemarker>
                          
                          
                                    <ftl:freemarker applyOnElement="example.model.OrderItem">
                                              <ftl:template>
                                                        <!--<order-item>
                                                                  <productId>${.vars["example.model.OrderItem"].productId}</productId>
                                                                  <quantity>${.vars["example.model.OrderItem"].quantity}</quantity>
                                                                  <price>${.vars["example.model.OrderItem"].price}</price>
                                                        </order-item>
                                              --></ftl:template>
                                    </ftl:freemarker>
                          

                           

                           

                          The problem had bean resolved,thank you Daniel.