简体   繁体   English

在Java中使用XMLEncoder进行序列化

[英]Serializing using XMLEncoder in java

After an hour of going through the wikis and conventions of using XMLEncoder in Java, I stil can't make any progress of serialising my SystemSnapshot class using XMLEncoder. 在经过一个小时的维基XMLEncoder在Java中使用XMLEncoder约定之后,我仍然无法使用XMLEncoder序列化SystemSnapshot类。 Can anyone tell me what's going wrong? 谁能告诉我怎么了? My resources are as follows : 我的资源如下:

package core;

import gui.SceneManager;
import java.io.Serializable;

/**
 *
 * @author Subhra
*/

public class SystemSnapshot implements Serializable{

private static final long serialVersionUID = -6637378268413872760L;

private final School school;

private final User[] users;

private final String colorName;

public SystemSnapshot(){
    school = SessionManager.getSchool();
    users = UserStore.getUsers();
    colorName = SceneManager.getColor();
}

public SystemSnapshot(School school, User[] users, String c) {
    this.school = school;
    this.users = users;
    this.colorName = c;
}

public School getSchool() {
    return school;
}

public User[] getUsers() {
    return users;
}

public String getColorName() {
    return colorName;
}

}

No errors in the terminal. 终端中没有错误。 But the output is like this : 但是输出是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_40" class="java.beans.XMLDecoder">
<object class="core.SystemSnapshot"/>
</java>

Anyone? 任何人?

After jtahlborn's answer i figured out and added all getters and setters. 在jtahlborn的回答之后,我弄清楚了并添加了所有的吸气剂和吸气剂。 Now i'm getting two types of outputs. 现在我得到两种类型的输出。 Case 1 : 情况1 :

XMLEncoder xml = new XMLEncoder(new FileOutputStream("try.xml"));
School ss = new School("S");
ss.addSubject(new Subject("BEnals"));
ss.addTeacher(new Teacher("Nil","T0",21,5000,new Subject("S")));
ss.addClass(new Standard(1,1));
ss.addStudent("STU0");
User u = new User(UserType.ADMIN,"Subhra","subhra","itsveryconfidential");
SystemSnapshot ss2 = new SystemSnapshot(ss,new User[]{u},"A string");
xml.writeObject(ss2);
xml.close();

is producing the right output. 正在产生正确的输出。 Whereas Case 2 : 情况2:

XMLEncoder oos = new XMLEncoder(new FileOutputStream("data.base"));
SystemSnapshot ss = new   SystemSnapshot(SessionManager.getSchool(),UserStore.getUsers(),SceneManager.getColor());
oos.writeObject(ss);
oos.close();

is still producing the output shown above. 仍在产生上面显示的输出。 What to do? 该怎么办?

Source Code for SessionManager and UserStore SessionManagerUserStore源代码

SessionManager.java SessionManager.java

package core;

import core.User.UserType;
public class SessionManager {

private static User currentuser;

private static School school;

public static void setSchool(School schol) {
     school = schol;
}

public static School getSchool() {
    return school;
}

public static User getCurrentuser() {
    return currentuser;
}

public static void setCurrentuser(User currentuser) {
    SessionManager.currentuser = currentuser;
}

public static UserType getCurrentUserType(){
    return currentuser.getType();
}

}

UserStore.java UserStore.java

package core;

public class UserStore {

private static User[] users = new User[0];

public static User[] getUsers() {
    return users;
}

public static void setUsers(User[] users) {
    UserStore.users = users;
}

public static void addUser(User u) {
    users = new BIO<User>().addToArray(users,u);
}

public static void removeUser(User u) {
    users = new BIO<User>().removeFromArray(users,u);
}

public static User getUser(String username){
    for(User u : users){
        if(u.getUsername().equals(username)){
            return u;
        }
    }
    return null;
}

}

What's wrong? 怎么了?

my guess is that your SystemSnapshot is not a valid java bean because it doesn't have setters for your properties. 我的猜测是您的SystemSnapshot不是有效的Java bean,因为它没有用于属性的设置器。 the XMLEncoder doesn't do the same magic that normal serialization does, it requires valid java beans (getters and setters for all properties). XMLEncoder的作用与普通序列化不同,它需要有效的Java bean(所有属性的getter和setter)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM