[Débutant] erreur de Compilation JavaXML
Bonjour
J’ai un problème avec « Serialisation XML dans la partie Objet vers XML» elle donne deux Exceptions:
Code:
1 2 3 4 5 6
|
java.lang.InstantiationException: Personne
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Personne);
Continuing ... |
voila le code: (je pence qu'il est selon les norme JavaBean)
la classe Personne
Code:
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
|
import javax.swing.JOptionPane;
import java.io.Serializable;
public class Personne implements Serializable {
private String nom, prenom;
/* L'attribut age ne sera pas sauvé, et il sera initialisé à null au chargement */
private transient int age;
/**
* @param args
*/
public void Personne(){
nom = "Pas de nom";
prenom = "Pas de prénom";
age = 0;
}
/*Remarque: il ne faut pas métre Voide dans la redifinistion du Constructeur !!*/
public Personne(String nom, String prenom, int age) {
this.nom = nom;
this.prenom = prenom;
this.age = age;
}
/* Affichage d'une personne en mode graphique */
public void AffichePersonne(){
JOptionPane.showMessageDialog(null,
"nom :"+ this.nom +"\nPrenom: "+this.prenom+"\nage: "+this.age
, "Personne",
JOptionPane.INFORMATION_MESSAGE);
}
/* Accesseurs et mutateurs */
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
} |
et la classe du Main():
Code:
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
|
import java.beans.XMLEncoder;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class TestSerialisationXML extends Exception {
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException {
/*
* On crée une nouvelle Personne et on l'affiche
* On crée un nouvel objet de type XMLEncoder avec comme paramètre
* un fichier XML
* On "écrit" notre objet dans le fichier XML avec writeObject()
* On vide le cache et force l'écriture
* On finit par fermer l'objet (on libère la mémoire)
*/
Personne p = new Personne("Loire", "Laguna", 27);
p.AffichePersonne();
XMLEncoder enc = new XMLEncoder(new FileOutputStream("fichier.xml"));
enc.writeObject(p);
enc.flush();
enc.close();
}
} |
voila a la fin le contenu du fichier XML:
Code:
1 2 3 4
|
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_06" class="java.beans.XMLDecoder">
</java> |
Si quelqu’un peu me dire :
Aux moins quand c’est Exception peu elle être levés. :(
Merci d’avance.