List of usage examples for javax.xml.bind JAXBContext newInstance
public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException
From source file:Main.java
public static <T> T fromXML(String xml, Class<T> valueType) { try {// ww w. jav a 2s . c o m JAXBContext context = JAXBContext.newInstance(valueType); Unmarshaller unmarshaller = context.createUnmarshaller(); return (T) unmarshaller.unmarshal(new StringReader(xml)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
From source file:Main.java
public static Object xmlStringToObject(String xmlString, Class clazz) throws Exception { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = new StringReader(xmlString); return unmarshaller.unmarshal(reader); }
From source file:Main.java
public static <T> String toXmlString(T obj, Class<T> type) { try {/*from w w w .ja va 2s . co m*/ JAXBContext jaxbContext = JAXBContext.newInstance(type); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream); return byteArrayOutputStream.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T toXML(Class<T> cls, String xml) { try {/*from w ww .ja v a 2s . c om*/ return (T) JAXBContext.newInstance(cls).createUnmarshaller().unmarshal(new StringReader(xml)); } catch (JAXBException e) { return (T) "<>"; } }
From source file:Main.java
public static byte[] serializerBytes(Object xmlObj) throws JAXBException { JAXBContext context = JAXBContext.newInstance(xmlObj.getClass()); Marshaller marshaller = context.createMarshaller(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(xmlObj, baos);//from w ww .j av a 2s .c om return baos.toByteArray(); }
From source file:Main.java
private static <T> Marshaller setUpMarshaller(Class<T> jaxbElementClass) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(jaxbElementClass); return jaxbContext.createMarshaller(); }
From source file:Main.java
public static <T> T loadXML(Class<T> type, File sourceFile) { try {/*from w ww .j a va 2s. com*/ JAXBContext context = JAXBContext.newInstance(type); return (T) context.createUnmarshaller().unmarshal(sourceFile); } catch (JAXBException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static <T> T parse(final Class<T> clazz, final InputStream inputStream) { try {/*from w ww . ja v a 2s.c o m*/ final JAXBContext jaxbContext = JAXBContext.newInstance(clazz); final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); final Object deserialized = jaxbUnmarshaller.unmarshal(inputStream); if (clazz.isAssignableFrom(deserialized.getClass())) { return clazz.cast(deserialized); } else { final JAXBElement<T> jaxbElement = (JAXBElement<T>) deserialized; return jaxbElement.getValue(); } } catch (final JAXBException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static File Marshall(Object object, String filePath) throws JAXBException { JAXBContext jc = JAXBContext.newInstance(object.getClass()); Marshaller u = jc.createMarshaller(); u.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); File f = new File(filePath); u.marshal(object, f);//from w w w .ja v a 2 s .c o m return f; }
From source file:Main.java
public static String saveObjectToXmlString(Object obj) throws JAXBException { final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); final StringWriter writer = new StringWriter(); m.marshal(obj, writer);/*from w ww . j a va2s.c o m*/ return writer.toString(); }