Java – Unmarshaller using JAXB binding

The generated package has been generated by JAXB and the classes where generated too.

/**
* This method open, read and unmarshall a file
* @param URLFichier url of the file to open
* @return an entity object
*/
public MyEntity readFile (String URLfile){

 try {
   JAXBContext jc = JAXBContext.newInstance("generated"); //this is the name of the generated package
   Unmarshaller unmarshaller = jc.createUnmarshaller();
 
   File fileXML = new File(URLfile);
   JAXBElement parseResult = (JAXBElement) unmarshaller.unmarshal(fileXML);
   MyEntity myEntity = (MyEntity) parseResult.getValue();
 } 
 catch (JAXBException ex) {
   System.out.println("Erreur JAXB : " + ex.getMessage());
 } 
 catch (ClassCastException ex) {
   System.out.println("Erreur lors du cast : " + ex.getMessage());
 }
}