Return to Snippet

Revision: 66522
at May 19, 2014 16:22 by alces


Initial Code
import javax.xml.bind.annotation.* 
import javax.xml.bind.JAXB

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) // without this annotation you'll see IllegalAnnotationException when try to marshall
public class Band {
	private String name
	private int establishedYear

	Band() {} // no-argument constaructor is required for POGO
	
	Band(String n, int e) {
		name = n
		establishedYear = e
	}
	
	String toString() {
		"$name founded in $establishedYear"
	}	
}

f = new File('pogo.xml')
// let's save
JAXB.marshal(new Band('The Little Willies', 2003), f)
// and then load
assert JAXB.unmarshal(f, Band.class).establishedYear == 2003

Initial URL
jaxb_groovy

Initial Description
Example of JAXB-annotated POGO, marshalled and unmarshalled via static methods of JAXB object.

Initial Title
Groovy object's marshalling/unmarshalling via JAXB

Initial Tags
xml, groovy

Initial Language
Groovy