; <header> <y:date xmlns:y="http://y">Wed Nov 15 13:45:28 EST 2006</y:date> <customer number="123123">Joe</customer> <privatePerson></privatePerson> </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>
The Java Model
(Not including getters and setters):
public class Order { private Header header; private List<OrderItem> orderItems; } public class Header { private Long customerNumber; private String customerName; } public class OrderItem { private long productId; private Integer quantity; private double price; }
The Binding Configuration and Execution Code
The configuration code (Note: Smooks instance defined and instantiated globally):
Smooks smooks = new Smooks(); Bean orderBean = new Bean(Order.class, "order", "/order"); orderBean.bindTo("header", orderBean.newBean(Header.class, "/order") .bindTo("customerNumber", "header/customer/@number") .bindTo("customerName", "header/customer") ).bindTo("orderItems", orderBean.newBean(ArrayList.class, "/order") .bindTo(orderBean.newBean(OrderItem.class, "order-item") .bindTo("productId", "order-item/product") .bindTo("quantity", "order-item/quantity") .bindTo("price", "order-item/price")) ); smooks.addVisitors(orderBean);
And the execution code:
JavaResult result = new JavaResult(); smooks.filterSource(new StreamSource(orderMessageStream), result); Order order = (Order) result.getBean("order");
@author
tom.fennelly@jboss.com
@see Value