首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未使用netBeans编写xmlEncoder

未使用netBeans编写xmlEncoder
EN

Stack Overflow用户
提问于 2010-05-20 04:11:18
回答 2查看 685关注 0票数 0

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

下面是对编写函数的调用:

代码语言:javascript
复制
dbManipulator.writeStudents(deps);

哪里

代码语言:javascript
复制
deps = new Hashtable<String, Department>();
dbManipulator = new DataBaseManipulator();

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

代码语言:javascript
复制
 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文件看起来仍与编写后的文件类似:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_18" class="java.beans.XMLDecoder"> 
 <object class="java.util.Hashtable"/> 
</java> 

提前谢谢你,

格雷格

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-20 04:20:22

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

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

票数 0
EN

Stack Overflow用户

发布于 2010-05-20 04:28:16

这是Student类的外观:

代码语言:javascript
复制
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;
    }
}

学生的课程是:

代码语言:javascript
复制
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;
    }


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2869036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档