1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package projetDev.moteur;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.filter.*;
import java.util.List;
import java.util.Iterator;
public class PartieSauvegarde {
static org.jdom.Document document;
static Element racine;
public static void sauvegarder(Partie p){
//On crée une instance de SAXBuilder
SAXBuilder sxb = new SAXBuilder();
try
{
//On crée un nouveau document JDOM avec en argument le fichier XML
//Le parsing est terminé ;)
document = sxb.build(new File("Exercice2.xml"));
racine = document.getRootElement();
}
catch(Exception e){}
//On initialise un nouvel élément racine avec l'élément racine du document.
Element partie = new Element("Partie");
racine.addContent(partie);
//Méthode définie dans la partie 3.2. de cet article
Attribute identifiant = new Attribute("nom",p.getNom()+"");
Attribute tailleDamier = new Attribute("tailleDamier",p.getTailleDamier()*p.getTailleDamier()+"");
partie.setAttribute(identifiant);
partie.setAttribute(tailleDamier);
try {
enregistreFichier("Exercice 2.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void enregistreFichier(String fichier) throws Exception
{
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, new FileOutputStream(fichier));
}
} |
Partager