Advertisement
Guest User

Untitled

a guest
May 17th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. package util;
  2.  
  3. import java.io.File;
  4. import javax.xml.bind.JAXBContext;
  5. import javax.xml.bind.JAXBException;
  6. import javax.xml.bind.Marshaller;
  7. import javax.xml.bind.Unmarshaller;
  8. import javax.xml.transform.Source;
  9. import javax.xml.transform.stream.StreamSource;
  10.  
  11. public class JaxParser {
  12.  
  13. public static <T> T unmarshal(Class<T> cl, File f) throws JAXBException
  14. {
  15. return unmarshal(cl, new StreamSource(f));
  16. }
  17. public static <T> T unmarshal(Class<T> cl, Source s) throws JAXBException
  18. {
  19. JAXBContext ctx = JAXBContext.newInstance(cl);
  20. Unmarshaller u = ctx.createUnmarshaller();
  21. return u.unmarshal(s, cl).getValue();
  22. }
  23.  
  24. public static <T> void marshal(T obj, File f) throws JAXBException
  25. {
  26. JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
  27. Marshaller m = ctx.createMarshaller();
  28. m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  29. m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  30. m.marshal(obj, f);
  31. }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement