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 Object convertXmlPathToObject(String xmlPath, Class clazz) { Object object = null;/*from w w w . j a v a 2s .c om*/ try { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); FileReader xmlReader = null; try { xmlReader = new FileReader(xmlPath); } catch (FileNotFoundException e) { e.printStackTrace(); } object = unmarshaller.unmarshal(xmlReader); } catch (JAXBException e) { e.printStackTrace(); } return object; }
From source file:Main.java
public static Object parseXmlStringToBeanByJAXB(String xml, Class clase) throws Exception { JAXBContext context = JAXBContext.newInstance(clase); Unmarshaller unmarshaller = context.createUnmarshaller(); InputStream is = null;//from www . j a v a 2 s . c o m try { is = new ByteArrayInputStream(xml.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (is == null) { return null; } JAXBElement elm = unmarshaller.unmarshal(new StreamSource(is), clase); return elm.getValue(); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXML(Class<?> class1, File file) throws JAXBException, IOException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (FileReader reader = new FileReader(file)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }/*from w w w .java2 s . c om*/ }
From source file:Main.java
public static Object convertXmlFileToObject(Class clazz, String xmlPath) { Object xmlObject = null;// w w w . j a va2 s .c o m try { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); FileReader fr = null; try { fr = new FileReader(xmlPath); } catch (FileNotFoundException e) { e.printStackTrace(); } xmlObject = unmarshaller.unmarshal(fr); } catch (JAXBException e) { e.printStackTrace(); } return xmlObject; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T xmlToBean(String xmlStr, Class<T> t) { try {/* w ww. j a v a 2s. c o m*/ JAXBContext context = JAXBContext.newInstance(t); Unmarshaller unmarshaller = context.createUnmarshaller(); T ts = (T) unmarshaller.unmarshal(new StringReader(xmlStr)); return ts; } catch (JAXBException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static Object toObject(Class className, String strXml) { Object object = null;/*w w w . j a v a 2s . com*/ 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
@SuppressWarnings("unchecked") public static <T> T xmlToBean(InputStream input, Class<T> t) { try {/*from www .j a va 2 s . c o m*/ JAXBContext context = JAXBContext.newInstance(t); Unmarshaller unmarshaller = context.createUnmarshaller(); T ts = (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8")); return ts; } catch (JAXBException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Object xmlTobean(String path, String classPath) { Object obj = null;//from w ww .jav a2 s.c o m 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
@SuppressWarnings("unchecked") public static <T> T unmarshallXml(Class<T> clazz, InputStream in) throws JAXBException, UnsupportedEncodingException { String className = clazz.getPackage().getName(); JAXBContext context = JAXBContext.newInstance(className); Unmarshaller unmarshaller = context.createUnmarshaller(); //Reader reader = new IgnoreIllegalCharactersXmlReader(in); Reader reader = new InputStreamReader(in, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); Object result = unmarshaller.unmarshal(is); //file); return (T) result; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T xml2Bean(String xml, Class<?> clazz) throws Exception { StringReader reader = null;/*from w w w.j av a2 s .c om*/ try { JAXBContext jc = JAXBContext.newInstance(new Class[] { clazz }); Unmarshaller m = jc.createUnmarshaller(); reader = new StringReader(xml); return (T) m.unmarshal(reader); } catch (Exception e) { } finally { close(reader); } return null; }