package restaurant; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class ReservationXML implements Serializable, ReservationFacade { ArrayList liste; String fichier = "C:\\fichier.xml"; public ReservationXML(){ liste = new ArrayList(); } public void init(){ SAXBuilder sxb = new SAXBuilder(); Document document=null; Element racine=null; Element temp=null; ReservationBean r1=null; try { document = sxb.build(new File(fichier)); racine = document.getRootElement(); for(Iterator iterator = racine.getChildren("reservation").iterator(); iterator.hasNext();){ temp = (Element)iterator.next(); r1 = new ReservationBean(); r1.setNom(temp.getAttributeValue("nom")); r1.setNumero(Integer.parseInt(temp.getAttributeValue("numero"))); this.addElement(r1); } //Si le fichier existe } catch (Exception e) { document = new Document(); } } public void destroy(){ XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); Element element = new Element("reservations"); Document document=new Document(); document.setRootElement(element); restaurant.ReservationBean temp = null; Attribute nom,numero; Element element_fils=null; for(Iterator iterator = this.getListe().iterator(); iterator.hasNext();){ temp = (restaurant.ReservationBean)iterator.next(); element_fils = new Element("reservation"); element.addContent(element_fils); nom = new Attribute("nom",temp.getNom()); element_fils.setAttribute(nom); numero = new Attribute("numero",""+temp.getNumero()); element_fils.setAttribute(numero); } //document.addContent(element); try { sortie.output(document, new FileOutputStream(fichier)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } /* (non-Javadoc) * @see restaurant.ReservationFacade#getListe() */ public ArrayList getListe() { return liste; } public void setListe(ArrayList liste) { this.liste = liste; } /* (non-Javadoc) * @see restaurant.ReservationFacade#addElement(restaurant.ReservationBean) */ public void addElement (ReservationBean reserv){ liste.add(reserv); } /* (non-Javadoc) * @see restaurant.ReservationFacade#removeReservation(restaurant.ReservationBean) */ public void removeReservation(ReservationBean r){ } /* (non-Javadoc) * @see restaurant.ReservationFacade#editReservation(restaurant.ReservationBean) */ public void editReservation (ReservationBean r){ } /* (non-Javadoc) * @see restaurant.ReservationFacade#addReservation(restaurant.ReservationBean) */ public void addReservation (ReservationBean r){ } }