Java XML JAXB Unserialize readObject(Class clazz, File file)

Here you can find the source of readObject(Class clazz, File file)

Description

read Object

License

Open Source License

Declaration

public static <T> T readObject(Class<T> clazz, File file) throws JAXBException 

Method Source Code

//package com.java2s;
/**//from w  ww .j  a v  a  2s. c  om
 Copyright (c) 2013, Amit Lieberman
All rights reserved.
    
           GNU LESSER GENERAL PUBLIC LICENSE
               Version 3, 29 June 2007
    
 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
    
    
  This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License
    
 * Created with love.
 * User: shpandrak
 * Date: 10/13/12
 * Time: 11:02
 */

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.InputStream;

public class Main {
    public static <T> T readObject(Class<T> clazz, File file) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(file);
    }

    public static <T> T readObject(Class<T> clazz, InputStream is) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(is);
    }
}

Related

  1. read(File file, Class typeParameterClass)
  2. readComplexProperty(String name, List objects, String methodName)
  3. readExternal(InputStream inputStream, Class clazz)
  4. readJAXB(Class clazz, InputStream is)
  5. readJaxbObject(InputStream inputStream, Class jaxbModelClass)
  6. readObjectFromInputStream(final InputStream inputStream, final Class expectedType)
  7. readObjectFromXml(Class jaxbBindClass, String xmlFileName)
  8. readXmlFileToObj(String path, String packageName)
  9. unserialize(T component, Class returnType)

  10. HOME | Copyright © www.java2s.com 2016