List of usage examples for javax.xml.bind JAXBContext createUnmarshaller
public abstract Unmarshaller createUnmarshaller() throws JAXBException;
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T convert2JavaBean(String xml, Class<T> c) { T t = null;/*from w w w .j a v a 2 s .co m*/ try { JAXBContext context = JAXBContext.newInstance(c); Unmarshaller unmarshaller = context.createUnmarshaller(); t = (T) unmarshaller.unmarshal(new StringReader(xml)); } catch (JAXBException e) { e.printStackTrace(); return t; } return t; }
From source file:Main.java
public static Object convertToPojoUsingFile(String fileName, Class... type) { Object result;/*from w w w . j a v a 2s. c o m*/ try { JAXBContext context = JAXBContext.newInstance(type); Unmarshaller unmarshaller = context.createUnmarshaller(); result = unmarshaller.unmarshal(new File(fileName)); } catch (JAXBException e) { throw new RuntimeException(e); } return result; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T xml2obj(String xml, Class<T> type) { T obj = null;// w w w. j a v a 2 s .c o m try { JAXBContext context = JAXBContext.newInstance(type); Unmarshaller unmarshaller = context.createUnmarshaller(); obj = (T) unmarshaller.unmarshal(new StringReader(xml)); } catch (JAXBException e) { e.printStackTrace(); } return obj; }
From source file:Main.java
public static JAXBElement<?> xmlToJaxb(Class<?> xmlClass, String xml) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); JAXBElement<?> element; try (StringReader reader = new StringReader(xml)) { element = (JAXBElement<?>) unmarshaller.unmarshal(reader); }// w w w .j a va 2s. c o m return element; }
From source file:Main.java
@SuppressWarnings({ "unchecked", "restriction" }) public static <T> T fromXML(String xml, Class<T> valueType) { try {/*from w ww . j a va 2 s. 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 final Object xml2obj(final Class<?> cls, final String xml) throws JAXBException { StringReader reader = new StringReader(xml); JAXBContext contextIn = JAXBContext.newInstance(cls); Unmarshaller marshallerIn = contextIn.createUnmarshaller(); return marshallerIn.unmarshal(reader); }
From source file:Main.java
public static <T> T deserialize(Class<T> res, InputStream is) throws JAXBException { String pkg = res.getPackage().getName(); JAXBContext jc = getCachedContext(pkg); Unmarshaller u = jc.createUnmarshaller(); u.setEventHandler(new DefaultValidationEventHandler()); return res.cast(u.unmarshal(is)); }
From source file:Main.java
/** * @param <T>/*from w w w. j av a 2s .co m*/ * the type we want to convert the XML into * @param c * the class of the parameterized type * @param xml * the instance XML description * @return a deserialization of the XML into an object of type T of class * class <T> * @throws javax.xml.bind.JAXBException */ @SuppressWarnings("unchecked") public static <T> T unmarshal(Class<T> c, String xml) throws JAXBException { T res; if (c == xml.getClass()) { res = (T) xml; } else { JAXBContext ctx = JAXBContext.newInstance(c); Unmarshaller marshaller = ctx.createUnmarshaller(); res = (T) marshaller.unmarshal(new StringReader(xml)); } return res; }
From source file:Main.java
@SuppressWarnings("unchecked") public static Object convertXmlStringToObject(String xml, Class clazz) { Object object = null;//www. j av a 2 s . c o m try { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); StringReader xmlReader = new StringReader(xml); object = unmarshaller.unmarshal(xmlReader); } catch (JAXBException e) { e.printStackTrace(); } return object; }
From source file:Main.java
public static <T> Object generateXML2Object(String str, Class cls) throws JAXBException { InputSource is;/*from ww w . j av a 2 s. c o m*/ Object obj = null; is = new InputSource(new StringReader(str)); JAXBContext jaxbContext = JAXBContext.newInstance(cls); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); obj = cls.cast(jaxbUnmarshaller.unmarshal(is)); return obj; }