package client.
utils;
import client.model.Etudiant;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
class EtudiantList {
private List<Etudiant> etudiants = new ArrayList<>();
@XmlElement(name = “etudiant”)
public List<Etudiant> getEtudiants() {
return etudiants;
public void setEtudiants(List<Etudiant> etudiants) {
this.etudiants = etudiants;
}
public class XMLHandler {
private static final String XML_FILE = “etudiants_en_attente.xml”;
public void saveToXML(Etudiant etudiant) {
try {
EtudiantList etudiantList = loadFromXMLFile();
if (etudiantList == null) {
etudiantList = new EtudiantList();
etudiantList.getEtudiants().add(etudiant);
JAXBContext context = JAXBContext.newInstance(EtudiantList.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(etudiantList, new File(XML_FILE));
} catch (JAXBException e) {
e.printStackTrace();
public List<Etudiant> loadFromXML() {
EtudiantList etudiantList = loadFromXMLFile();
return etudiantList != null ? etudiantList.getEtudiants() : new ArrayList<>();
private EtudiantList loadFromXMLFile() {
try {
File file = new File(XML_FILE);
if (!file.exists()) {
return new EtudiantList();
JAXBContext context = JAXBContext.newInstance(EtudiantList.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (EtudiantList) unmarshaller.unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
return null;
public void clearXMLFile() {
try {
EtudiantList emptyList = new EtudiantList();
JAXBContext context = JAXBContext.newInstance(EtudiantList.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(emptyList, new File(XML_FILE));
} catch (JAXBException e) {
e.printStackTrace();