Java XML JAXB Unmarshaller unmarshal(InputStream content, Class expectedType)

Here you can find the source of unmarshal(InputStream content, Class expectedType)

Description

Unmarshal XML data from InputStream by expectedType and return the resulting content tree.

License

Open Source License

Parameter

Parameter Description
content InputStream with data to unmarshal
expectedType appropriate JAXB mapped class

Exception

Parameter Description
RuntimeException If any unexpected errors occur while unmarshalling

Return

Java content rooted by JAXB Element

Declaration

public static <T> T unmarshal(InputStream content, Class<T> expectedType) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import com.google.common.collect.Maps;
import javax.xml.bind.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import java.io.InputStream;

import java.util.Map;

public class Main {
    private static final Map<Class<?>, JAXBContext> CONTEXTS = Maps.newHashMap();

    /**//from  ww w  . j a v  a  2  s .c o  m
     * <p>Unmarshal XML data from {@code InputStream} by <tt>expectedType</tt> and return the
     * resulting content tree.</p>
     *
     * @param content      {@code InputStream} with data to unmarshal
     * @param expectedType appropriate JAXB mapped class
     * @return Java content rooted by JAXB Element
     * @throws RuntimeException If any unexpected errors occur while unmarshalling
     */
    public static <T> T unmarshal(InputStream content, Class<T> expectedType) {
        return unmarshal(new StreamSource(content), expectedType);
    }

    /**
     * <p>Unmarshal XML data from the specified XML Source by <tt>expectedType</tt> and return the
     * resulting content tree.</p>
     *
     * @param source       the XML Source to unmarshal XML data from (providers are only required to support SAXSource,
     *                     DOMSource, and StreamSource)
     * @param expectedType appropriate JAXB mapped class
     * @return Java content rooted by JAXB Element
     * @throws RuntimeException If any unexpected errors occur while unmarshalling
     */
    public static <T> T unmarshal(Source source, Class<T> expectedType) {
        try {
            Unmarshaller unmarshaller = getContext(expectedType).createUnmarshaller();
            return unmarshaller.unmarshal(source, expectedType).getValue();
        } catch (JAXBException e) {
            throw new RuntimeException("Failed to unmarshal object", e);
        }
    }

    private static JAXBContext getContext(Class<?> clazz) {
        JAXBContext context = CONTEXTS.get(clazz);
        if (context == null) {
            try {
                context = JAXBContext.newInstance(clazz);
            } catch (JAXBException e) {
                throw new RuntimeException("Failed to create JAXBContext for type " + clazz, e);
            }
            CONTEXTS.put(clazz, context);
        }
        return context;
    }
}

Related

  1. unmarshal(File f)
  2. unmarshal(final Class clazz, String json)
  3. unmarshal(final String xml, Class clazz, InputStream inputSchema)
  4. unmarshal(final String xml, final Class clazz)
  5. unmarshal(InputSource inputSource, Class clazz)
  6. unmarshal(InputStream in, Class... boundClasses)
  7. unmarshal(InputStream inputStream, Class entityClass)
  8. unmarshal(InputStream inputStream, Class type)
  9. unmarshal(InputStream is, Class clazz)