0% found this document useful (0 votes)
4 views

XMLHandler.java

The document defines a Java class for handling XML operations related to a list of 'Etudiant' (student) objects. It includes methods for saving, loading, and clearing student data in an XML file using JAXB for marshalling and unmarshalling. The 'EtudiantList' class serves as a container for a list of 'Etudiant' objects, facilitating their serialization and deserialization to and from an XML format.

Uploaded by

corbeille.omega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

XMLHandler.java

The document defines a Java class for handling XML operations related to a list of 'Etudiant' (student) objects. It includes methods for saving, loading, and clearing student data in an XML file using JAXB for marshalling and unmarshalling. The 'EtudiantList' class serves as a container for a list of 'Etudiant' objects, facilitating their serialization and deserialization to and from an XML format.

Uploaded by

corbeille.omega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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();

You might also like