Posted By

narkisr on 08/30/10


Tagged

java xml JAXB


Versions (?)

JAXB marshling


 / Published in: Java
 

Simple Annotated Class to XML marshling using JAXB

  1. public class Play {
  2. public static void generateXML(Object rootElement,
  3. Writer writer) throws JAXBException {
  4.  
  5. if (rootElement == null)
  6. throw new IllegalArgumentException("rootElement is null");
  7. if (writer == null)
  8. throw new IllegalArgumentException("writer is null");
  9.  
  10. JAXBContext context = JAXBContext.newInstance(rootElement.getClass());
  11. Marshaller marshaller = context.createMarshaller();
  12. marshaller.setProperty(
  13. Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  14. marshaller.marshal(rootElement, writer);
  15. }
  16.  
  17. public static void main(String[] args) throws JAXBException {
  18. final StringWriter writer = new StringWriter();
  19. generateXML(new AnnotatedClass(), writer);
  20. System.out.println( writer.getBuffer().toString());
  21. }
  22.  
  23. }

Report this snippet  

You need to login to post a comment.