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 parseObjToXmlString(Object obj) { if (obj == null) { return noResult; }//from ww w.j a va2s . c om StringWriter sw = new StringWriter(); JAXBContext jAXBContext; Marshaller marshaller; try { jAXBContext = JAXBContext.newInstance(obj.getClass()); marshaller = jAXBContext.createMarshaller(); marshaller.marshal(obj, sw); return sw.toString(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return noResult; }
From source file:Main.java
public static <T> T unmarshal(JAXBContext context, Class<T> clazz, String source) throws JAXBException { if (source == null) { return null; }//from w w w. j a v a 2s . c om StringReader reader = new StringReader(source); if (context == null) { context = JAXBContext.newInstance(clazz); } Unmarshaller un = context.createUnmarshaller(); return (T) un.unmarshal(reader); }
From source file:Main.java
public static void saveInstance(OutputStream outputStream, Object instance) throws JAXBException, IOException { Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING); marshaller.marshal(instance, outputStream); outputStream.flush();/*from w w w . java2 s . c o m*/ }
From source file:Main.java
public static Marshaller createMarshaller(String pack, Schema schema) { JAXBContext jaxbContext = null; try {//from w w w. java2 s.co m jaxbContext = JAXBContext.newInstance(pack); Marshaller marsh = jaxbContext.createMarshaller(); if (schema != null) { marsh.setSchema(schema); // marsh.setEventHandler( new DefaultValidationEventHandler() { // @Override // public boolean handleEvent( ValidationEvent event ) { // return super.handleEvent( event ); // } // }); } marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); return marsh; } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String getXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema) throws JAXBException { String packageName = versioningInfo.getValue().getClass().getPackage().getName(); JAXBContext context = JAXBContext.newInstance(packageName); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatXml); if (schema != null) { marshaller.setSchema(schema);// ww w . ja v a2s . co m } ByteArrayOutputStream oStream = new ByteArrayOutputStream(); marshaller.marshal(versioningInfo, oStream); return oStream.toString(); }
From source file:Main.java
private static JAXBContext newContext(Class<?> classJAXB) throws Exception { return JAXBContext.newInstance(classJAXB); }
From source file:Main.java
private static String parseObject2XmlString(Object object) { if (object == null) { return noResult; }//from w w w . j av a 2 s . co m StringWriter sw = new StringWriter(); JAXBContext jaxbContext; Marshaller marshaller; try { jaxbContext = JAXBContext.newInstance(object.getClass()); marshaller = jaxbContext.createMarshaller(); marshaller.marshal(object, sw); return sw.toString(); } catch (JAXBException e) { e.printStackTrace(); return noResult; } }
From source file:Main.java
public static String objectToXml(Object object) { if (null == object) { return NO_RESULT; }//ww w. j a v a 2 s .c o m StringWriter sw = new StringWriter(); JAXBContext jAXBContent = null; Marshaller marshaller = null; try { jAXBContent = JAXBContext.newInstance(object.getClass()); marshaller = jAXBContent.createMarshaller(); marshaller.marshal(object, sw); return sw.toString(); } catch (JAXBException e) { e.printStackTrace(); } return NO_RESULT; }
From source file:Main.java
public static String marshalToString(Class<?> klass, Object obj) throws JAXBException { try {//from www . j av a 2 s. com 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."); } StringWriter sw = new StringWriter(); String result = null; try { marshaller.marshal(obj, sw); result = sw == null ? null : sw.toString(); } catch (JAXBException jaxbe) { jaxbe.printStackTrace(); } return result; }
From source file:Main.java
/** * Object to XML/* w ww . j a va2 s. c o m*/ * @param object * @return */ public static String convertToXML(Object object) { try { if (!mMap.containsKey(object.getClass())) { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); mMap.put(object.getClass(), marshaller); } StringWriter stringWriter = new StringWriter(); mMap.get(object.getClass()).marshal(object, stringWriter); return stringWriter.getBuffer().toString(); } catch (JAXBException e) { e.printStackTrace(); } return null; }