xmlEncoder 未在 netBeans 中写入

发布于 2024-09-02 16:44:05 字数 1043 浏览 10 评论 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> 

提前致谢,

Greg

I am trying to use the xmlEncoder to write to xml file in net-beans but it doesnt work.

Here is the call to the writing function:

dbManipulator.writeStudents(deps);

where

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

Department is an class-object I made, and here is writeStudents method which is located in the DataBaseManipulator class:

 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()

Any ideas why it isnt working? I tried changing the hashtable to vector but still the xml file looks like that after the writing:

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

Thanks in advance,

Greg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熊抱啵儿 2024-09-09 16:44:15

这是 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;
    }
}

Student 类是:

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;
    }


}

This is how the Student class looks:

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;
    }
}

and Student class is:

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;
    }


}
弥枳 2024-09-09 16:44:13

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

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

Is Students following Java Beans spec? Don't forget that if your object only has default data, nothing will be written except an element representing that there is such an
object. That's because the encoder doesn't write any data that the default constructor can take care of.

Check if your hashTable really contains student objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文