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> void saveObject(T object, Class<T> typeClass, URL path) { try {/*from w w w. j a va 2 s. c o m*/ File file = new File(path.toURI()); JAXBContext jaxbContext = JAXBContext.newInstance(typeClass); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, file); } catch (JAXBException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> String marshal(T object) throws IOException, JAXBException { Class<T> clzz = (Class<T>) object.getClass(); JAXBContext context;/*ww w .j av a 2 s . c om*/ context = JAXBContext.newInstance(clzz); Marshaller m = context.createMarshaller(); ByteArrayOutputStream os = new ByteArrayOutputStream(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(object, os); return os.toString(); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> Object xmlToObject(String xmlContent, Class<T> clazz) throws JAXBException { ByteArrayInputStream xmlContentBytes = new ByteArrayInputStream(xmlContent.getBytes()); JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setSchema(null); //note: setting schema to null will turn validator off Object unmarshalledObj = unmarshaller.unmarshal(xmlContentBytes); Object xmlObject = clazz.cast(unmarshalledObj); return (T) xmlObject; }
From source file:Main.java
public static <T> T xml2Obj(String xmlString, Class<T> clazz) throws RuntimeException { T c = null;/* ww w. java2 s .c om*/ Reader reader = null; try { reader = new StringReader(xmlString); Unmarshaller unMarshaller = JAXBContext.newInstance(clazz).createUnmarshaller(); c = (T) unMarshaller.unmarshal(reader); } catch (JAXBException ex) { throw new RuntimeException(ex); } finally { try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } return c; }
From source file:Main.java
public static String toXml(Object object) { final StringWriter out = new StringWriter(); JAXBContext context = null;/* ww w.j a v a 2s . c om*/ try { context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(object, new StreamResult(out)); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } return out.toString(); }
From source file:Main.java
/** * genXml/* w ww . j a v a 2 s . c o m*/ * * @param o Object Class. * @param path : example c:\path\006DS_AMS20130630.xml */ public static void genXml(Object o, String path) { try { // create JAXB context and initializing Marshaller JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // for getting nice formatted output jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //specify the location and name of xml file to be created File XMLfile = new File(path); // Writing to XML file jaxbMarshaller.marshal(o, XMLfile); // Writing to console jaxbMarshaller.marshal(o, System.out); } catch (JAXBException e) { // some exception occured e.printStackTrace(); } }
From source file:Main.java
private static <T> JAXBContext getContext(Class<T> c) throws JAXBException { if (!map.containsKey(c)) { System.out.println("-" + c.getSimpleName()); map.put(c, JAXBContext.newInstance(c)); }/*from w w w . jav a2 s . c om*/ return (JAXBContext) map.get(c); }
From source file:Main.java
public static String parseObject2XmlString(Object object) { if (object == null) { return noResult; }//from w w w.j a v a2 s . com StringWriter sw = new StringWriter(); JAXBContext jAXBContent; Marshaller marshaller; try { jAXBContent = JAXBContext.newInstance(object.getClass()); marshaller = jAXBContent.createMarshaller(); marshaller.marshal(object, sw); return sw.toString(); } catch (Exception e) { e.printStackTrace(); return noResult; } }
From source file:Main.java
public static Object xml2Bean(Class<?> zClass, String xml) { Object obj = null;/*from ww w.j a va 2 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()); Unmarshaller um = context.createUnmarshaller(); obj = (Object) um.unmarshal(iStream); return obj; } catch (JAXBException e) { e.printStackTrace(); } return obj; }
From source file:Main.java
public static void marshal(Class<?> klass, Object obj, OutputStream os) throws JAXBException { try {// w w w. ja va 2 s .co m context = JAXBContext.newInstance(klass); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); unmarshaller = context.createUnmarshaller(); } catch (JAXBException e) { throw new RuntimeException( "There was a problem creating a JAXBContext object for formatting the object to XML."); } try { marshaller.marshal(obj, os); } catch (JAXBException jaxbe) { jaxbe.printStackTrace(); } }