xmlEncoder不在netBeans中编写

时间:2010-05-19 20:11:18

标签: java xml netbeans hashtable encoder

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

这是对写作功能的调用:

dbManipulator.writeStudents(deps);

,其中

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

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

 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> 

提前致谢,

格雷格

2 个答案:

答案 0 :(得分:0)

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

检查您的hashTable是否真的包含学生对象。

答案 1 :(得分: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;
    }


}
相关问题