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
public static <T> T asObject(String xml, Class<T> clazz) { try {/*from w w w . j av a 2 s. c o m*/ JAXBContext jaxbContext = null; jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = null; unmarshaller = jaxbContext.createUnmarshaller(); return (T) unmarshaller.unmarshal(new StringReader(xml)); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Loads the object from the file.//from w w w .j a v a2 s. c om * * @param <T> the object type. * @param path the XML file. * @param clazz the class of the object. * @return the corresponding object. */ public static <T> T loadObject(Path path, Class<T> clazz) { T result; if (path == null || clazz == null) { throw new RuntimeException("The path to file or class is null!"); } try { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); result = (T) jaxbUnmarshaller.unmarshal(path.toFile()); } catch (Exception ex) { throw new RuntimeException("Error loading the xml from path " + path.toString(), ex); } return result; }
From source file:Main.java
public static <T> Object JAXBUnmarshalling(Class insClass, String fileSource) throws JAXBException { JAXBContext jAXBContext = JAXBContext.newInstance(insClass); Unmarshaller unmarshaller = jAXBContext.createUnmarshaller(); File file = new File(fileSource); Object object = unmarshaller.unmarshal(file); return object; }
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
/** * Convert XML data to a bean using JAXB. * //from w ww . j a va 2 s .c o m * @param b * The bean, represented as XML. * @param implClass * The implementation class of the bean. * @param bc * Additional classes to add to the JAXB context. * @return The bean, unmarshalled from the XML data using JAXB. */ public static <T> T unmarshal(String b, Class<T> implClass, Class<?>... bc) { Class<?>[] bind; if (bc.length > 1) { bind = new Class<?>[bc.length + 1]; bind[0] = implClass; for (int i = 0; i < bc.length; i++) { bind[i + 1] = bc[i]; } } else { bind = new Class<?>[] { implClass, }; } ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes()); try { JAXBContext jaxbContext = JAXBContext.newInstance(bind); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return implClass.cast(unmarshaller.unmarshal(bais)); } catch (JAXBException e) { throw new IllegalStateException("Error unmarshalling", e); } }
From source file:Main.java
public static Object importXmlJAXB(Class<?>[] clazz, File file) throws JAXBException { JAXBContext jc = getJAXBContext(clazz); Unmarshaller u = jc.createUnmarshaller(); return u.unmarshal(file); }
From source file:Main.java
public static Object importXmlJAXB(Class<?>[] clazz, URL url) throws JAXBException { JAXBContext jc = getJAXBContext(clazz); Unmarshaller u = jc.createUnmarshaller(); return u.unmarshal(url); }
From source file:Main.java
public static Object importXmlJAXB(Class<?>[] clazz, InputStream in) throws JAXBException { JAXBContext jc = getJAXBContext(clazz); Unmarshaller u = jc.createUnmarshaller(); return u.unmarshal(in); }
From source file:Main.java
public static Object importXmlJAXB(Class<?>[] clazz, Reader in) throws JAXBException { JAXBContext jc = getJAXBContext(clazz); Unmarshaller u = jc.createUnmarshaller(); return u.unmarshal(in); }
From source file:Main.java
public static <T> T unserializer(Class<T> clazz, InputStream xmlInput) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); @SuppressWarnings("unchecked") T obj = (T) unmarshaller.unmarshal(xmlInput); return obj;/*from w w w .ja va 2 s . c o m*/ }