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 Object deserialize(Element elmnt, Class cls) throws JAXBException { final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller(); return um.unmarshal(elmnt); }
From source file:de.bloxel.engine.util.JAXBUtils.java
public static <T> T unmarschal(final InputStream inputStream, final Class<T> aClass) { try {//from w w w.j a va 2 s. c o m // http://jaxb.java.net/faq/index.html#classloader final JAXBContext jc = JAXBContext.newInstance(getPackageCanonicalName(aClass)); final Unmarshaller unmarshaller = jc.createUnmarshaller(); // http://jaxb.java.net/guide/Unmarshalling_is_not_working__Help_.html unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); final XMLInputFactory staxFactory = XMLInputFactory.newInstance(); final XMLStreamReader xmlReader = staxFactory.createXMLStreamReader(inputStream); return unmarshaller.unmarshal(xmlReader, aClass).getValue(); } catch (final JAXBException e) { throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e); } catch (final XMLStreamException e) { throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e); } }
From source file:Main.java
public static void saveInstance(OutputStream outputStream, URL schemaURL, String schemaName, Object instance) throws JAXBException, IOException {/*from w w w . j a v a2 s .c o m*/ Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaURL + " " + schemaName); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(instance, outputStream); outputStream.flush(); }
From source file:Main.java
public static <T> void serialize(T object, OutputStream resultStream) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, resultStream); }
From source file:Main.java
public static <T> Document marshal(Object jaxbElement, Class<T> jaxbFactory) throws JAXBException { if (!(jaxbElement instanceof JAXBElement<?>)) { throw new JAXBException("Must be a instance of JAXBElement<?>"); }/*from w w w .j av a2 s. c o m*/ Document doc = null; try { JAXBContext jc = JAXBContext.newInstance(jaxbFactory); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.newDocument(); marshaller.marshal(jaxbElement, doc); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return doc; }
From source file:Main.java
/** * XML to Object/*from www . java 2s . c o m*/ * @param <T> T * @param clazz clazz * @param reader reader * @return T */ @SuppressWarnings("unchecked") public static <T> T convertToObject(Class<T> clazz, Reader reader) { try { Map<Class<?>, Unmarshaller> uMap = uMapLocal.get(); if (!uMap.containsKey(clazz)) { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); uMap.put(clazz, unmarshaller); } return (T) uMap.get(clazz).unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * * @param obj//from w w w . ja va2s. c o m * @return * @throws JAXBException * @throws ParserConfigurationException */ public static Document jaxbToDocument(Object obj) throws JAXBException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.newDocument(); // Marshal the Object to a Document final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller(); m.marshal(obj, doc); return doc; }
From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java
public static CXML unmarshallCXMLFromFile(final String relativeFilePath) throws FileNotFoundException { final InputStream fileInputStream = PunchOutUtils.class.getClassLoader() .getResourceAsStream(relativeFilePath); if (fileInputStream == null) { throw new FileNotFoundException("Could not find file [" + relativeFilePath + "]"); }/*from w ww. ja va2s . c o m*/ try { final JAXBContext jaxbContext = JAXBContext.newInstance(CXML.class); final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return (CXML) unmarshaller.unmarshal(fileInputStream); } catch (final JAXBException e) { throw new PunchOutException(e.getErrorCode(), e.getMessage()); } }
From source file:Main.java
/** * Converting DOM Element object into JAXB OASIS XML Object * @param <T>//from ww w .ja v a2s .c om * @param cls * @param domElement * @return */ public static <T> T marshal(Class<T> cls, Element domElement) { try { JAXBContext jc = JAXBContext.newInstance(cls); javax.xml.bind.Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBElement<T> jaxbObject = unmarshaller.unmarshal(domElement, cls); T object = jaxbObject.getValue(); return object; } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:com.azaptree.services.commons.xml.jaxb.JAXBContextCache.java
/** * If the JAXBContext is not cached, then it will create a new instance and cache it. * /*from www . j a v a 2 s.c o m*/ * @param contextPath * REQUIRED * @return */ public static JAXBContext get(final String contextPath) { Assert.hasText(contextPath, "contextPath is required"); JAXBContext ctx = jaxbContexts.get(contextPath); if (ctx == null) { try { ctx = JAXBContext.newInstance(contextPath); } catch (final JAXBException e) { throw new IllegalArgumentException( "Failed to create JAXBContext - invalid JAXB context path: " + contextPath, e); } jaxbContexts.put(contextPath, ctx); LoggerFactory.getLogger(JAXBContextCache.class).info("cached : {}", contextPath); } return ctx; }