xmlEncoder 未在 netBeans 中写入
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Student 类的样子:
Student 类是:
This is how the Student class looks:
and Student class is:
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 anobject. 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.