-
1. Re: How can i use smooks to transform a java object to a formed xml?
beve Mar 18, 2011 12:38 AM (in response to zhujiang001)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?
zhujiang001 Mar 18, 2011 12:45 AM (in response to beve)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?
zhujiang001 Mar 18, 2011 12:59 AM (in response to zhujiang001)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?
beve Mar 18, 2011 1:54 AM (in response to zhujiang001)1 of 1 people found this helpfulIn 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
-
5. Re: How can i use smooks to transform a java object to a formed xml?
zhujiang001 Mar 18, 2011 2:45 AM (in response to beve)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?
zhujiang001 Mar 18, 2011 2:50 AM (in response to zhujiang001)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?
zhujiang001 Mar 18, 2011 2:58 AM (in response to zhujiang001)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?
zhujiang001 Mar 18, 2011 4:07 AM (in response to zhujiang001)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.
-
9. How can i use smooks to transform a java object to a formed xml?
zhujiang001 Mar 20, 2011 9:26 PM (in response to zhujiang001)help
-
10. Re: How can i use smooks to transform a java object to a formed xml?
zhujiang001 Mar 20, 2011 11:48 PM (in response to zhujiang001)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.