0

我正在尝试使用 xmlEncoder 写入 net-beans 中的 xml 文件,但它不起作用。

这是对写入函数的调用:

dbManipulator.writeStudents(deps);

在哪里

deps = new Hashtable<String, Department>();
dbManipulator = new DataBaseManipulator();

Department 是我创建的一个类对象,这里是位于 DataBaseManipulator 类中的 writeStudents 方法:

 public void writeStudents(Hashtable<Integer, Student> students)
    {
            XMLEncoder encoder = null;
            try
            {
                encoder = new XMLEncoder(new FileOutputStream(".\\test\\Students.xml"));
            }
            catch(Exception e){}
            encoder.writeObject(students);
            encoder.close();
    }//end of function writeStudents()

任何想法为什么它不起作用?我尝试将哈希表更改为向量,但写入后 xml 文件仍然是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_18" class="java.beans.XMLDecoder"> 
 <object class="java.util.Hashtable"/> 
</java> 

提前致谢,

格雷格

4

2 回答 2

0

是否Students遵循 Java Beans 规范?不要忘记,如果您的对象只有默认数据,则除了表示存在此类对象的元素外,不会写入任何内容。这是因为编码器不会写入任何默认构造函数可以处理的数据。

检查您的哈希表是否真的包含学生对象。

于 2010-05-19T20:20:22.140 回答
0

这是 Student 类的外观:

package Application;
import java.util.*;

public class AcceptedStudent extends Student{

    private String depName;
    private Hashtable<String, CourseDetails> coursesDetails; // key - courseName string, value - courseDetails object

    public AcceptedStudent(int newId, String first, String last, String newDep)
    {
        super(newId, first, last);
        depName = newDep;
        coursesDetails = new Hashtable<String, CourseDetails>();
    }

    public AcceptedStudent(int newId, String first, String last, String newDep, Hashtable<String, CourseDetails> newCourseDetails)
    {
        super(newId, first, last);
        depName = newDep;
        coursesDetails = newCourseDetails;
    }

     // Function that checks if the student took the course and got higher than 56
    public boolean checkSuccessInCourse(String courseName)
    {
        // If the student took the pre course
        if (coursesDetails.containsKey(courseName))
        {
            // If the student got grade higher than 56 in this course
            if (((CourseDetails)coursesDetails.get(courseName)).getGrade() >= 56)
            {
                return true;
            }
            return false;
        }
        return false;
    }

    public void addCourseDetails(CourseDetails cd)
    {
        coursesDetails.put(cd.getCourseName(), cd);
    }

    public Hashtable getCourseDetails()
    {
        return coursesDetails;
    }
    public String getDep()
    {
        return depName;
    }
}

和学生类是:

package Application;

public class Student {

    private int id;
    private String fName;
    private String lName;
    private boolean status;


    public Student(int newId, String first, String last)
    {
        id = newId;
        fName = first;
        lName = last;
        status = false;
    }

    public int getId()
    {
        return id;
    }

    public String getFirstName()
    {
        return fName;
    }

    public String getLastName()
    {
        return lName;
    }

    public boolean getStatus()
    {
        return status;
    }


}
于 2010-05-19T20:28:15.553 回答