Java tutorial
import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlRootElement; public class Main { public static void main(String args[]) throws Exception { Order o = new Order(); o.setCustId(123); o.setDescription("New order"); o.setOrderDate(new Date()); List<Item> items = new ArrayList<Item>(); Item i = new Item(); i.setName("PC"); i.setQty(10); items.add(i); i = new Item(); i.setName("Box"); i.setQty(4); items.add(i); o.setItems(items); // Write it JAXBContext ctx = JAXBContext.newInstance(Order.class); Marshaller m = ctx.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter sw = new StringWriter(); m.marshal(o, sw); sw.close(); System.out.println(sw.toString()); // Read it back JAXBContext readCtx = JAXBContext.newInstance(Order.class); Unmarshaller um = readCtx.createUnmarshaller(); Order newOrder = (Order) um.unmarshal(new StringReader(sw.toString())); System.out.println(newOrder); } @XmlRootElement public static class Order { int custId; Date orderDate; String description; List<Item> items; public void setCustId(int custId) { this.custId = custId; } public void setOrderDate(Date orderDate) { this.orderDate = orderDate; } public void setDescription(String description) { this.description = description; } public void setItems(List<Item> items) { this.items = items; } public int getCustId() { return custId; } public Date getOrderDate() { return orderDate; } public String getDescription() { return description; } public List<Item> getItems() { return items; } public String toString() { return "Order: " + custId + " - " + orderDate + " - " + description + " - " + items; } } public static class Item { String name; private int qty; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } public String toString() { return "Item:" + name + " - " + qty; } } }