Java tutorial
//package com.java2s; import java.io.File; import java.io.FileReader; import java.io.IOException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.sax.SAXSource; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; public class Main { public static <T> T readXML(Class<?> class1, String filepath) throws JAXBException, IOException, SAXException, ParserConfigurationException { return readXML(class1, new File(filepath)); } @SuppressWarnings("unchecked") public static <T> T readXML(Class<?> class1, File file) throws JAXBException, IOException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (FileReader reader = new FileReader(file)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; } } }