4.7. Binding Data
- Using the Order XML message, look at the full XML-to-Java binding configuration. Here are the Java objects that you must populate from that XML message (the "getters" and "setters" are not shown):
public class Order { private Header header; private List<OrderItem> orderItems; } public class Header { private Date date; private Long customerNumber; private String customerName; private double total; } public class OrderItem { private long productId; private Integer quantity; private double price; }
- Use this configuration to bind the data from the order XML to the object model:
<?xml version="1.0"?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.4.xsd"> (1) <jb:bean beanId="order" class="com.acme.Order" createOnElement="order"> (1.a) <jb:wiring property="header" beanIdRef="header" /> (1.b) <jb:wiring property="orderItems" beanIdRef="orderItems" /> </jb:bean> (2) <jb:bean beanId="header" class="com.acme.Header" createOnElement="order"> (2.a) <jb:value property="date" decoder="Date" data="header/date"> <jb:decodeParam name="format">EEE MMM dd HH:mm:ss z yyyy</jb:decodeParam> </jb:value> (2.b) <jb:value property="customerNumber" data="header/customer/@number" /> (2.c) <jb:value property="customerName" data="header/customer" /> (2.d) <jb:expression property="total" execOnElement="order-item" > += (orderItem.price * orderItem.quantity); </jb:expression> </jb:bean> (3) <jb:bean beanId="orderItems" class="java.util.ArrayList" createOnElement="order"> (3.a) <jb:wiring beanType="com.acme.OrderItem" /> <!-- Could also wire using beanIdRef="orderItem" --> </jb:bean> (4) <jb:bean beanId="orderItem" class="com.acme.OrderItem" createOnElement="order-item"> (4.a) <jb:value property="productId" data="order-item/product" /> (4.b) <jb:value property="quantity" data="order-item/quantity" /> (4.c) <jb:value property="price" data="order-item/price" /> </jb:bean> </smooks-resource-list>