为什么我不能在XML中序列化我的类?

时间:2014-11-17 14:40:43

标签: java xmlencoder

目前我在一个java项目中工作。我需要为学校开发一个管理校园,学生和老师的应用程序。 我需要在这个项目中做的事情是使用与每个校园相对应的xml(使用XMLencoder)数据进行序列化。

我可以序列化除以下类之外的所有类: InternalTeacherExternalExternalTeacherInternalTeach

请告诉我有什么问题? 提前致谢

这是我在xml文件中得到的(我的意思是在序列化之后):

<java version="1.8.0_20" class="java.beans.XMLDecoder"><object class="infomaniak.Campus"><void property="capacite"><int>100</int></void><void property="region"><string>Bouches de rhônes</string></void><void property="ville"><string>Polytech</string></void></object><array class="infomaniak.Student" length="3"><void index="0"><object class="infomaniak.Student"><void property="ID"><int>991</int></void><void property="dateInscription"><object class="java.util.Date"><long>1415919600000</long></object></void><void property="nom"><string>ID BESSLAM</string></void><void property="prenom"><string>Said</string></void></object></void><void index="1"><object class="infomaniak.Student"><void property="ID"><int>992</int></void><void property="dateInscription"><object class="java.util.Date"><long>1415919600000</long></object></void><void property="nom"><string>OUHAKI</string></void><void property="prenom"><string>Karim</string></void></object></void><void index="2"><object class="infomaniak.Student"><void property="ID"><int>993</int></void><void property="dateInscription"><object class="java.util.Date"><long>1415919600000</long></object></void><void property="nom"><string>Catherine</string></void><void property="prenom"><string>Jennifer</string></void></object></void></array><array class="infomaniak.ExternalTeacherInternal" length="2"><void index="0"><object class="infomaniak.InternalTeacher"/></void><void index="1"><object class="infomaniak.ExternalTeacher"/></void></array><array class="infomaniak.ExternalTeacher" length="1"><void index="0"><object class="infomaniak.ExternalTeacher"/></void></array><array class="infomaniak.InternalTeacher" length="1"><void index="0"><object class="infomaniak.InternalTeacher"/></void></array></java>

这是我的源代码: 校园:

public class Campus implements Serializable{    
String ville;   
String region;
int capacite=0;

int nbrStudent=0;
int nbrTeacher=0;
int nbrExternalTeacher = 0;
int nbrInternalTeacher = 0;   

Student[] listStudent = new Student [100];
ExternalTeacherInternal[] listTeacher = new ExternalTeacherInternal[100];



static int nbrCampus = 0;

public Campus (String v, String r, int c) {
    this.ville = v;
    this.region = r;
    this.capacite = c;
    nbrCampus++;
}

public Campus (){

}

 public void setVille(String ville) {
    this.ville = ville;
}

 public String getVille() {
    return this.ville;
}

public void setRegion(String region) {
    this.region = region;
}

public String getRegion() {
    return this.region;
}

public void setCapacite(int capacite) {
    this.capacite = capacite;
}

public int getCapacite() {
    return this.capacite;
}

public int getNbrStudent() {
    return nbrStudent;
}

public int getNbrTeacher() {
    return nbrTeacher;
}

public void addStudent (Student s) {

}
//public void removeStudent (Student s) {}
//public int getStudent();

//public void addTeacher (Teacher t) {}
//public void removeTeacher (Teacher t) {}
//public int getTeacher();

}

学生:

public class Student implements Serializable {
int ID;
String prenom;
String nom;
Date dateInscription;

public Student (){

}

public Student (int i, String p, String n) {
    this.ID = i;
    this.prenom = p;
    this.nom = n;
}

public Student (int i, String p, String n, Date dateInsc) {
    this.ID = i;
    this.prenom = p;
    this.nom = n;
    this.dateInscription = dateInsc;
}

public void setID (int i) {
    this.ID = i;
}

public int getID () {
    return this.ID;
}

public void setPrenom (String p) {
    this.prenom = p; 
}   

public String getPrenom ()
{return prenom;}

public void setNom (String n) {
    this.nom = n;
}    

public String getNom() {
    return nom;
}

public void setDateInscription(Date dateInscription) {
    this.dateInscription = dateInscription;
}

public Date getDateInscription() {
    return dateInscription;
}

}

师:

abstract class Teacher implements Serializable {
protected int ID;
protected String prenom;
protected String nom;
protected int salaire;

public Teacher () {

}

public Teacher (int i, String p, String n){
    this.ID = i;
    this.prenom = p;
    this.nom = n;        
}           

protected void setID( int i){
    this.ID = i;
}

protected int getID() {
    return this.ID;
}

protected void setPrenom(String p) {
    this.prenom = p;
}

protected String getPrenom() {
    return this.prenom;
}

protected void setNom(String n) {
    this.nom = n;
}

protected String getNom() {
    return this.nom;
}

abstract void setSalaire(int s);

abstract int getSalaire();

abstract void fixerSalaire();

}

InternalTeacherExternal:

public class ExternalTeacherInternal extends Teacher implements Serializable {

public ExternalTeacherInternal (int i, String p, String n) {
    super(i, p, n);
}
public ExternalTeacherInternal () {

}

void setSalaire(int s){}

int getSalaire(){return 0;}

void fixerSalaire(){}

}

ExternalTeacher:

public class ExternalTeacher extends ExternalTeacherInternal implements Serializable {
int salaire;

public ExternalTeacher (int i, String p, String n, int s) {
    super(i, p, n);
    this.salaire = s;
}

public ExternalTeacher (int i, String p, String n) {
    super(i, p, n);
}

public ExternalTeacher (){}

void setSalaire(int s) {
    salaire = s;
}

int getSalaire() {
    return salaire;
}        

void fixerSalaire(){
    System.out.print("Veuillez entrer le salaire: ");
    try {
        int s = System.in.read();
        this.salaire = s;
    } catch (IOException ex) {
        Logger.getLogger(ExternalTeacher.class.getName()).log(Level.SEVERE, null, ex);
    }        

}

}

InternalTeacher:

public class InternalTeacher extends ExternalTeacherInternal implements Serializable {
int salaire = 1200;

public InternalTeacher (int i, String p, String n, int s) {
    super(i, p, n);
    this.salaire = s;
}

public InternalTeacher (int i, String p, String n) {
    super(i, p, n);
}

public InternalTeacher (){}

void setSalaire(int s) {
    salaire = s;
}

int getSalaire() {
    return salaire;
}  

 void fixerSalaire(){
    System.out.print("Veuillez entrer le salaire: ");
    try {
        int s = System.in.read();
        this.salaire = s;
    } catch (IOException ex) {
        Logger.getLogger(ExternalTeacher.class.getName()).log(Level.SEVERE, null, ex);
    }   

}

}

主:

public class Infomaniak implements Serializable {

public static void main(String[] args) throws IOException, ParseException {

    Campus camp = new Campus("Polytech", "Bouches de rhônes", 100);

    Student[] stu = new Student[3];
    stu[0] = new Student(991, "Said", "ID BESSLAM", new SimpleDateFormat("dd/MM/yyyy").parse("14/11/2014"));
    stu[1] = new Student(992, "Karim", "OUHAKI", new SimpleDateFormat("dd/MM/yyyy").parse("14/11/2014"));
    stu[2] = new Student(993, "Jennifer", "Catherine", new SimpleDateFormat("dd/MM/yyyy").parse("14/11/2014"));

    ExternalTeacherInternal[] teach = new ExternalTeacherInternal[2];
    teach[0] = new InternalTeacher(551, "Fouzia", "OUNNAR", 10000);
    teach[1] = new ExternalTeacher(552, "Aline", "Cauvin", 24000);

    System.out.println("ID: "+stu[0].getID()+"    Nom:"+stu[0].getNom()+"     Prenom:"+stu[0].getPrenom()+"    Date d'inscription: "+stu[0].getDateInscription());
    System.out.println("ID: "+teach[0].getID()+"    Nom:"+teach[0].getNom()+"     Prenom:"+teach[0].getPrenom());
    System.out.println("ID: "+teach[1].getID()+"    Nom:"+teach[1].getNom()+"     Prenom:"+teach[1].getPrenom());


    camp.nbrStudent = 3;
    camp.nbrTeacher = 2; 
    camp.nbrInternalTeacher = 1;
    camp.nbrExternalTeacher = 1;

    System.out.println("camp.nbrStudent: "+camp.nbrStudent+"   "+"camp.nbrTeacher: "+camp.nbrTeacher+"    "+"camp.nbrExternalTeacher: "+camp.nbrExternalTeacher+"     "+"camp.nbrInternalTeacher: "+camp.nbrInternalTeacher);
    File list = new File("C:\\Users\\said\\Desktop\\", "test.xml");

    System.out.println("\n\n\n******** Sauvegarde *********");
    SaveLoad serialize = new SaveLoad(camp, stu, teach, list);
    serialize.Save();
}

0 个答案:

没有答案