Groovy object's marshalling/unmarshalling via JAXB


/ Published in: Groovy
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. import javax.xml.bind.annotation.*
  2. import javax.xml.bind.JAXB
  3.  
  4. @XmlRootElement
  5. @XmlAccessorType(XmlAccessType.FIELD) // without this annotation you'll see IllegalAnnotationException when try to marshall
  6. public class Band {
  7. private int establishedYear
  8.  
  9. Band() {} // no-argument constaructor is required for POGO
  10.  
  11. Band(String n, int e) {
  12. name = n
  13. establishedYear = e
  14. }
  15.  
  16. String toString() {
  17. "$name founded in $establishedYear"
  18. }
  19. }
  20.  
  21. f = new File('pogo.xml')
  22. // let's save
  23. JAXB.marshal(new Band('The Little Willies', 2003), f)
  24. // and then load
  25. assert JAXB.unmarshal(f, Band.class).establishedYear == 2003

URL: jaxb_groovy

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.