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> Object generateXML2Object(String str, Class cls) throws JAXBException { InputSource is;/*from w w w. j a v a2s . com*/ 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 String bean2Xml(Object bean) throws Exception { StringWriter writer = null;//w ww .j a v a 2 s . c o m try { JAXBContext jc = JAXBContext.newInstance(new Class[] { bean.getClass() }); Marshaller m = jc.createMarshaller(); m.setProperty("jaxb.fragment", Boolean.valueOf(true)); writer = new StringWriter(); m.marshal(bean, writer); return prefix + writer.toString(); } catch (Exception e) { e.printStackTrace(); } finally { close(writer); } return null; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T xmlToBean(String xmlStr, Class<T> t) { try {//from w w w . java 2 s . c om JAXBContext context = JAXBContext.newInstance(t); Unmarshaller unmarshaller = context.createUnmarshaller(); return (T) unmarshaller.unmarshal(new StringReader(xmlStr)); } catch (JAXBException e) { e.printStackTrace(); return null; } }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T xmlToBean(String xmlStr, Class<T> t) { try {/*w ww.j a v a 2 s. 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
/** * /*from ww w .j a v a 2 s. c om*/ * @param xmlData * @return * @return * @return * @throws JAXBException */ @SuppressWarnings("unchecked") public static <T> T xmlStringToPojo(String xmlData, Class<T> targetClass) throws JAXBException { JAXBContext context = JAXBContext.newInstance(targetClass); StringReader reader = new StringReader(xmlData); Unmarshaller unmarshaller = context.createUnmarshaller(); return (T) unmarshaller.unmarshal(reader); }
From source file:Main.java
public static String convertObjectToXML(Object object) { JAXBContext jaxbContext = null; Marshaller jaxbMarshaller = null; StringWriter stringWriter = new StringWriter(); try {//from w w w .j a va 2 s .c o m jaxbContext = JAXBContext.newInstance(object.getClass()); jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); jaxbMarshaller.marshal(object, stringWriter); } catch (JAXBException e) { e.printStackTrace(); } String xmlString = stringWriter.toString(); return xmlString; }
From source file:Main.java
public static String parseObjectToXml(Object obj) { if (obj == null) { return NO_RESULT; }//from w w w . ja v a2s.c om StringWriter sw = new StringWriter(); try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(obj, sw); return sw.toString(); } catch (JAXBException e) { e.printStackTrace(); } return NO_RESULT; }
From source file:Main.java
public static Object xml2BeanUtf8(Class<?> zClass, String xml) { Object obj = null;/* w ww . j a v a2 s .c om*/ JAXBContext context = null; if (null == xml || "".equals(xml) || "null".equalsIgnoreCase(xml) || xml.length() < 1) return obj; try { context = JAXBContext.newInstance(zClass); InputStream iStream = new ByteArrayInputStream(xml.getBytes("UTF-8")); Unmarshaller um = context.createUnmarshaller(); obj = (Object) um.unmarshal(iStream); return obj; } catch (Exception e) { e.printStackTrace(); } return obj; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T unmarshaller(String xml, Class<?> T) { JAXBContext jc;/*from w ww. java 2 s .c o m*/ Unmarshaller unmarshaller; Object o = null; try { jc = JAXBContext.newInstance(T); unmarshaller = jc.createUnmarshaller(); o = unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); } catch (JAXBException e) { e.printStackTrace(); } return (T) o; }
From source file:Main.java
public static <T> T loadObject(Class<T> typeClass, URL path) { T object = null;/* w w w . ja v a 2 s . c o m*/ try { File file = new File(path.toURI()); JAXBContext jaxbContext = JAXBContext.newInstance(typeClass); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); object = (T) jaxbUnmarshaller.unmarshal(file); } catch (JAXBException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } return object; }