daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest May 17th, 2018 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top