Java XML JAXB String to Object fromXML(final byte[] data, final Class type)

Here you can find the source of fromXML(final byte[] data, final Class type)

Description

Converts byte data representing XML of a JAXB object to an object.

License

Open Source License

Parameter

Parameter Description
data the data to be converted to an object
type the class that is to be created and instantiated from the data

Return

the object that was derived from the XML data

Declaration

public static Object fromXML(final byte[] data, final Class<?> type) 

Method Source Code


//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;
        }
    }
}

Related

  1. fromStringToObject(String xmlString, Class xmlObjClass)
  2. fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml)
  3. fromXML(Class clazz, InputStream is)
  4. fromXml(Class tClass, InputStream inputStream)
  5. fromXml(File xmlPath, Class type)
  6. fromXml(final Reader reader, final Class dtoClass)
  7. fromXml(InputStream input, Class clazz)
  8. fromXml(InputStream xml, Class objectClass)
  9. fromXml(Reader xml, Class clazz)