List of usage examples for javax.xml.bind Unmarshaller unmarshal
public Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;
From source file:Main.java
/** * @param <T>/* www . j a v a 2s. c om*/ * 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
/** * Parse XML using JAXB and a model class. * // w ww. ja v a 2s .c o m * @param in an input stream * @return the requested object * @param cls the root class * @throws JAXBException thrown on an error */ @SuppressWarnings("unchecked") public static <T> T parseJaxb(Class<T> cls, InputStream in) throws JAXBException { JAXBContext context = JAXBContext.newInstance(cls); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Unmarshaller um = context.createUnmarshaller(); return (T) um.unmarshal(in); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T convertToObject(String xml, Class<T> type) { StringReader sr = new StringReader(xml); try {/*from w w w . ja va 2 s. c o m*/ JAXBContext jAXBContext = JAXBContext.newInstance(type); Unmarshaller unmarshaller = jAXBContext.createUnmarshaller(); return (T) unmarshaller.unmarshal(sr); } catch (JAXBException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static <T> Object generateXML2Object(String str, Class cls) throws JAXBException { InputSource is;// w w w .j a v a 2 s . c om 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; }
From source file:Main.java
public static <T> T converyToJavaBean(String xmlStr, Class<T> c) { T t = null;//w ww . j ava 2s .com try { JAXBContext context = JAXBContext.newInstance(c); Unmarshaller unmarshaller = context.createUnmarshaller(); t = (T) unmarshaller.unmarshal(new StringReader(xmlStr)); } catch (Exception e) { e.printStackTrace(); } return t; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T unmarshal(Class<T> clazz, InputStream input) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); return (T) jaxbUnmarshaller.unmarshal(input); }
From source file:Main.java
public static Object xmlTobean(String path, String classPath) { Object obj = null;//from w w w . j a va 2s . com Class<?> obClass = null; try { obClass = Class.forName(classPath); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { File file = new File(path); JAXBContext context = JAXBContext.newInstance(obClass); Unmarshaller unmarshaller = context.createUnmarshaller(); obj = unmarshaller.unmarshal(file); System.out.println(obj); } catch (JAXBException e) { e.printStackTrace(); } return obj; }
From source file:Main.java
public static Object toObject(Class className, String strXml) { Object object = null;// w w w . j a va2s. co m StringReader reader = null; try { reader = new StringReader(strXml); JAXBContext context = JAXBContext.newInstance(className); Unmarshaller unmarshaller = context.createUnmarshaller(); object = unmarshaller.unmarshal(reader); } catch (Exception e) { } finally { if (reader != null) { reader.close(); reader = null; } } return object; }
From source file:Main.java
public static <T> T unmarshall(String file, Class<T> desiredClass, Class context) throws Exception { JAXBContext ctx = JAXBContext.newInstance(context); Unmarshaller unMarshaller = ctx.createUnmarshaller(); JAXBElement<T> object = (JAXBElement<T>) unMarshaller.unmarshal(new File(file)); return object.getValue(); }
From source file:Main.java
public static Object unmarshal(String xml, Class<?> classObj) { Object obj;/*from ww w .j ava 2s . c om*/ try { JAXBContext jaxbContext = getJAXBContext(classObj); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); obj = unmarshaller.unmarshal(new StringReader(xml)); return obj; } catch (JAXBException e) { e.printStackTrace(); } return null; }