Java XML JAXB Unserialize loadObject(Class typeClass, URL path)

Here you can find the source of loadObject(Class typeClass, URL path)

Description

load Object

License

Artistic License

Declaration

public static <T> T loadObject(Class<T> typeClass, URL path) 

Method Source Code


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

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    public static <T> T loadObject(Class<T> typeClass, URL path) {
        T object = null;//from ww w .j  a  v a2 s  .c  o m

        try {
            File file = new File(path.toURI());
            JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            object = (T) jaxbUnmarshaller.unmarshal(file);
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return object;
    }
}

Related

  1. load(Class clazz, InputStream is)
  2. load(Class clazz, InputStream is)
  3. load(File f, Class... args)
  4. load(final String xml, final Class clazz)
  5. load(InputStream input, Class type)
  6. loadObject(Path path, Class clazz)
  7. loadXML(Class type, File sourceFile)
  8. loadXml(File file, Class requireType)
  9. loadXML(Object object, String fileNamePath)