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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
public class Annee {
private List annee = new ArrayList();
public Annee() {
super();
}
public void addCanari(canari unCanari) {
annee.add(unCanari);
}
public List getAnnee() {
return annee;
}
public void setAnnee(List annee) {
annee = annee;
}
public void ecrire(){
File file = new File("c:\\travail\\test.xml");
Writer writer = null;
try {
writer = new FileWriter(file);
//getsion du mapping
Mapping mapping = new Mapping();
mapping.loadMapping("C:\\travail\\fichierMapping.xml"); // c'est ton fichier de mapping
//gestion du marshaller
Marshaller marshaller = new Marshaller(writer);
//on affecte au marshaller le mapping
marshaller.setMapping(mapping);
marshaller.setEncoding("ISO-8859-1");
marshaller.marshal(this);
} catch (MarshalException me) {
me.printStackTrace();
System.exit(0);
} catch (ValidationException ve) {
ve.printStackTrace();
System.exit(0);
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(0);
} catch (org.exolab.castor.mapping.MappingException ioe) {
ioe.printStackTrace();
System.exit(0);
}
}
public static Annee lire(){
File file = new File("c:\\travail\\test.xml");
Annee liste=null;
try {
FileReader reader = new FileReader(file);
//getsion du mapping
Mapping mapping = new Mapping();
mapping.loadMapping("C:\\travail\\fichierMapping.xml"); // c'est ton fichier de mapping
//gestion du marshaller
Unmarshaller unmarshaller = new Unmarshaller(mapping);
//on affecte au marshaller le mapping
liste = (Annee)unmarshaller.unmarshal(Annee.class,reader);
} catch (MarshalException e) {
e.printStackTrace();
System.exit(0);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(0);
} catch (ValidationException e) {
e.printStackTrace();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
} catch (MappingException e) {
e.printStackTrace();
System.exit(0);
}
return liste;
}
public static void main(String[] args) {
//exemple d'utilisation
//création
canari titi=new canari("titi","mâle",1212,58,2004);
canari mimie=new canari("mimie","femelle",5608,32,2003);
Annee liste= new Annee();
liste.addCanari(titi);
liste.addCanari(mimie);
//ecriture
liste.ecrire();
//lecture l'une année
Annee lecture = Annee.lire();
for(Iterator ite=lecture.getAnnee().iterator();ite.hasNext();){
canari unCanari = (canari) ite.next();
unCanari.affiche();
}
//modification d'un canari (ici le premier)
canari unCanari = (canari) lecture.getAnnee().get(0);
unCanari.setNom("grosMinet"); // changementdu nom par exemple (titi devient grosMinet ;-))
lecture.ecrire();// enregistrement de la modification dans le fichier xml
//.....
}
} |