Java XML JAXB Unmarshaller unmarshall(InputStream toUnmarshall, Class clazz)

Here you can find the source of unmarshall(InputStream toUnmarshall, Class clazz)

Description

UnMarshalls a String to the specidfied object type.

License

Open Source License

Parameter

Parameter Description
T the type of object being unmarshalled
toUnmarshall the String to unmarshall
clazz the class of the unMarshsalled object

Exception

Parameter Description
JAXBException if there is an error unmarshalling the String

Return

the Object

Declaration

@SuppressWarnings("unchecked")
public static final <T> T unmarshall(InputStream toUnmarshall, Class<T> clazz) throws IOException 

Method Source Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.bind.JAXBContext;

public class Main {
    /**/*from   w  w  w  .  j ava  2s. co  m*/
     * UnMarshalls a String to the specidfied object type. <br/>
     * Example: <code>
     * Environment unmarshalled = JaxbUtil.<Environment>unmarshall(marshalled, Environment.class);
     * </code>
     * 
     * @param <T>
     *            the type of object being unmarshalled
     * @param toUnmarshall
     *            the String to unmarshall
     * @param clazz
     *            the class of the unMarshsalled object
     * @return the Object
     * @throws JAXBException
     *             if there is an error unmarshalling the String
     */
    @SuppressWarnings("unchecked")
    public static final <T> T unmarshall(InputStream toUnmarshall, Class<T> clazz) throws IOException {
        Object unmarshalled = null;
        try {
            JAXBContext ctx = JAXBContext.newInstance(clazz.getPackage().getName());
            unmarshalled = ctx.createUnmarshaller().unmarshal(new InputStreamReader(toUnmarshall));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return (T) unmarshalled;
    }
}

Related

  1. unmarshalInstance(final String data, final Class expectedType)
  2. unmarshall(Class clazz, String string)
  3. unmarshall(Class c, Object o)
  4. unmarshall(Class cls, Element domRequest)
  5. unmarshall(InputStream is, Class clz)
  6. unmarshall(JAXBContext c, Element e)
  7. unmarshall(String cntxtPkg, InputStream in)
  8. unmarshall(String file, Class desiredClass, Class context)
  9. unmarshall(String xml, Class... classesToBeBound)