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 String marshaller(Object o, Class<?> T) { JAXBContext jc;/* w ww . j a va 2 s.c o m*/ Marshaller marshaller; StringWriter writer = new StringWriter(); try { jc = JAXBContext.newInstance(T); marshaller = jc.createMarshaller(); marshaller.marshal(o, writer); } catch (JAXBException e) { e.printStackTrace(); } return writer.toString(); }
From source file:Main.java
public static void Object2XmlFile(Object ob, String path) throws JAXBException, FileNotFoundException { Class local = ob.getClass();//from w ww . ja v a 2s .co m JAXBContext context = JAXBContext.newInstance(new Class[] { local }); Marshaller marshaller = context.createMarshaller(); // marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", // paramNamespacePrefixMapper); // marshaller.setProperty("jaxb.formatted.output", // Boolean.valueOf(true)); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(ob, new FileOutputStream(new File(path))); }
From source file:Main.java
public static Object convertToPojoUsingString(String xml, Class... type) { Object result;//from w ww . jav a2 s . co m try { JAXBContext context = JAXBContext.newInstance(type); Unmarshaller unmarshaller = context.createUnmarshaller(); InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); result = unmarshaller.unmarshal(stream); } catch (JAXBException e) { throw new RuntimeException(e); } return result; }
From source file:Main.java
public static Object toObject(Class className, String strXml) { Object object = null;/*w w w. jav a2 s . c o 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 String toXml(Class className, Object object) { String strXml = ""; StringWriter writer = null;/*from w w w . ja v a2s. c o m*/ try { writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(className); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writer); strXml = writer.toString(); writer.flush(); strXml = strXml.replace("<", "<"); strXml = strXml.replace(">", ">"); } catch (Exception e) { } finally { if (writer != null) { try { writer.close(); writer = null; } catch (Exception e) { } } } return strXml; }
From source file:Main.java
public static String bean2Xml(Object bean) { String xmlString = null;/* w w w .ja v a 2s .co m*/ JAXBContext context; StringWriter writer; if (null == bean) return xmlString; try { context = JAXBContext.newInstance(bean.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);// //m.setProperty(Marshaller.JAXB_ENCODING, "gb2312");// //m.setProperty(Marshaller.JAXB_ENCODING, "GBK");// m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// m.setProperty(Marshaller.JAXB_FRAGMENT, false);// writer = new StringWriter(); m.marshal(bean, writer); xmlString = writer.toString(); return xmlString; } catch (Exception e) { e.printStackTrace(); } return xmlString; }
From source file:Main.java
public static String obj2Xml(Object obj, String encoding) { String result = null;//from w ww . j a v a 2 s .c o m try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
/** * @param file//from w ww. j a v a 2 s.c o m * @return * @throws JAXBException */ public static Object unmarshalXML(Class clasz, InputStream file) throws JAXBException { //1. We need to create JAXContext instance JAXBContext jaxbContext = JAXBContext.newInstance(clasz); //2. Use JAXBContext instance to create the Unmarshaller. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); //3. Use the Unmarshaller to unmarshal the XML document to get an instance of JAXBElement. return (file != null) ? unmarshaller.unmarshal(file) : null; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T unmarshall(String xml, Class<T> cl) throws JAXBException, UnsupportedEncodingException { JAXBContext jaxbCtx = JAXBContext.newInstance(cl); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); return (T) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8"))); }
From source file:Main.java
public static Object fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml) { JAXBContext context = null;/*from ww w. jav a 2s . c o m*/ try { context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(new StringReader(stringXml))); } catch (JAXBException e) { e.printStackTrace(); } return null; }