Here you can find the source of fromXML(final byte[] data, final Class> type)
Parameter | Description |
---|---|
data | the data to be converted to an object |
type | the class that is to be created and instantiated from the data |
public static Object fromXML(final byte[] data, final Class<?> type)
//package com.java2s; import java.io.ByteArrayInputStream; import java.util.HashMap; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; public class Main { /**//from w w w . j a v a 2 s . c o m * Map of classes and respective JAXBContexts. */ private static Map<Class<?>, JAXBContext> serMap = new HashMap<Class<?>, JAXBContext>(); /** * Converts byte data representing XML of a JAXB object to an object. * @param data * the data to be converted to an object * @param type * the class that is to be created and instantiated from the data * @return * the object that was derived from the XML data */ public static Object fromXML(final byte[] data, final Class<?> type) { try { final JAXBContext context = getContext(type); final Unmarshaller unMarshal = context.createUnmarshaller(); final JAXBElement<?> temp = unMarshal.unmarshal(new StreamSource(new ByteArrayInputStream(data)), type); return temp.getValue(); } catch (final JAXBException exception) { throw new IllegalStateException(exception); } } /** * Gets the JAXBContext for the passed in class. * @param theClass * the class to retrieve the JAXBContext for * @return * the JAXBContext that has been found or created * @throws JAXBException * throws a JAXBException if context cannot be created */ private static JAXBContext getContext(final Class<?> theClass) throws JAXBException { if (serMap.containsKey(theClass)) { return serMap.get(theClass); } else { final JAXBContext jContext = JAXBContext.newInstance(theClass); serMap.put(theClass, jContext); return jContext; } } }