Java tutorial
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class Main { public static <T> T loadXmlFromFile(Class<T> clazz, String filename) throws FileNotFoundException, JAXBException { return unmarshal(clazz, new FileInputStream(filename)); } public static <T> T loadXmlFromFile(Class<T> clazz, File file) throws FileNotFoundException, JAXBException { return unmarshal(clazz, new FileInputStream(file)); } public static <T> T loadXmlFromFile(Class<T> clazz, InputStream is) throws JAXBException { return unmarshal(clazz, is); } @SuppressWarnings("unchecked") public static <T> T unmarshal(Class<T> clazz, InputStream input) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); return (T) jaxbUnmarshaller.unmarshal(input); } }