Java XML JAXB Unserialize parse(final Class clazz, final InputStream inputStream)

Here you can find the source of parse(final Class clazz, final InputStream inputStream)

Description

parse

License

LGPL

Declaration

public static <T> T parse(final Class<T> clazz, final InputStream inputStream) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import javax.xml.bind.*;

import java.io.InputStream;

public class Main {
    public static <T> T parse(final Class<T> clazz, final InputStream inputStream) {
        try {//from w ww  .  j  a  v a 2  s  .co m
            final JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            final Object deserialized = jaxbUnmarshaller.unmarshal(inputStream);
            if (clazz.isAssignableFrom(deserialized.getClass())) {
                return clazz.cast(deserialized);
            } else {
                final JAXBElement<T> jaxbElement = (JAXBElement<T>) deserialized;
                return jaxbElement.getValue();
            }
        } catch (final JAXBException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. loadObject(Path path, Class clazz)
  2. loadXML(Class type, File sourceFile)
  3. loadXml(File file, Class requireType)
  4. loadXML(Object object, String fileNamePath)
  5. loadXMLFromString(Object object, String line)
  6. parseIsoDate(String lexicalDate)
  7. parseJaxb(Class cls, InputStream in)
  8. parseXml(final Class klass, final Reader xmlReader)
  9. parseXML(String content, Class clazz)