Posted By

alces on 05/19/14


Tagged

xml groovy marshal JAXB unmarshal pogo


Versions (?)

Groovy object's marshalling/unmarshalling via JAXB


 / Published in: Groovy
 

URL: jaxb_groovy

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

  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

Report this snippet  

You need to login to post a comment.