JAXB is pretty painless, and comes with Java 6, and it has a low footprint.
You get this for the price of one annotation.
Order.java
package xxx;
import java.util.Date;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public 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;
}
}
Item.java:
package xxx;
public 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;
}
}
Test.java:
package xxx;
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;
public class Test {
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("Duck Soup");
i.setQty(10);
items.add(i);
i = new Item();
i.setName("Carrots");
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);
}
}
Results:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
<custId>123</custId>
<description>New order</description>
<items>
<name>Duck Soup</name>
<qty>10</qty>
</items>
<items>
<name>Carrots</name>
<qty>4</qty>
</items>
<orderDate>2010-06-16T18:12:06.870-07:00</orderDate>
</order>
Order: 123 - Wed Jun 16 18:12:06 PDT 2010 - New order - [Item:Duck Soup - 10, Item:Carrots - 4]