jOOX has been awfully quiet lately due to increased development focus in jOOQ. Nevertheless, the jOOX feature roadmap is full of promising new features. Unlike its inspiration jquery, jOOX is positioning itself in the Java world, where many XML API’s already exist. One of the most important XML APIs in Java is JAXB, a very simple means of mapping XML to Java through annotations (see also my blog stream on the subject of Annotatiomania™).
Let’s have a look at this small XML document
Typically, we would write a Java class like this to map to the above XML document:
@XmlRootElement
public class Customer {
String name;
int age;
int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
}
And then, we would marshal / unmarshal the above using the following code snippet:
JAXB.marshal(new Customer(), System.out);
Customer c = JAXB.unmarshal(xml, Customer.class);
JAXB and jOOX
This is very neat and convenient. But it gets even better when JAXB is used along with jOOX. Have a look at the following piece of code:
// Use the $ method to wrap a JAXB-annotated object:
$(new Customer());
// Navigate to customer elements in XML:
String id = $(new Customer()).id();
String name = $(new Customer()).find("name").text();
// Modify the XML structure, and unmarshal it again into
// a JAXB-annotated object:
Match match = $(new Customer());
match.find("name").text("Peter");
Customer modified = match.unmarshalOne(Customer.class);