无法让 JavaBeans 写入 XML

Trouble Gettting JavaBeans to write to XML

我的代码将姓名写入 XML 文档,但不写入任何考试成绩。即使我更改名称,测试分数也始终输出 0。如果能帮助我弄清楚为什么会这样,我将不胜感激。我已将 class 附加到 main 方法,将 class 附加到构造函数。感谢您的帮助!

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Studentp194Runner {

public static void main(String[] args)
{           
    Studentp194 s1 = new Studentp194();

    Scanner reader = new Scanner(System.in);
    System.out.print("Enter student name: ");
    s1.setName(reader.nextLine());
    System.out.print("Enter the student's first score: ");
    s1.setScore(1, reader.nextInt());
    System.out.print("Enter the student's second score: ");
    s1.setScore(2, reader.nextInt());
    System.out.print("Enter the student's third score: ");
    s1.setScore(3, reader.nextInt());

    try
    {
        FileOutputStream fos = new FileOutputStream(new File("./student.xml"));
        XMLEncoder encoder = new XMLEncoder(fos);
        encoder.writeObject(s1);
        encoder.close();
        fos.close();
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }

    try
    {
        FileInputStream fis = new FileInputStream(new File("./student.xml"));
        XMLDecoder decoder = new XMLDecoder(fis);
        Studentp194 p2 = (Studentp194)decoder.readObject();
        decoder.close();
        fis.close();
        System.out.println("Student 1 name: " + p2.getName());
        System.out.println("Test 1: " + p2.getScore(1));
        System.out.println("Test 2: " + p2.getScore(2));
        System.out.println("Test 3: " + p2.getScore(3));

    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }   
}

}

public class Studentp194 {
//instance variable
private String name;
private int test1;
private int test2;
private int test3;

//constructor method

public Studentp194()
{

}

public Studentp194(String name, int test1, int test2, int test3)
{
    this.name = name;
    this.test1 = test1;
    this.test2 = test2;
    this.test3 = test3;
}

//Other Methods

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public void setScore(int i, int score){
    if (i == 1)
        test1 = score;
    if(i == 2)
        test2 = score;
    else
        test3 = score;
}

public int getScore(int i)
{
    if (i == 1)
        return test1;
    if (i == 2)
        return test2;
    else
        return test3;
}

public int getAverage()
{
    int average = (int)Math.round((test1 + test2 + test3) / 3);
    return average;
}

public int getHighScore()
{
    int highScore = test1;
    if(test2 > highScore)
        highScore = test2;
    if(test3 > highScore)
        highScore = test3;
    return highScore;
}

public String toString()
{
    String str = "Name: " + name + "\n" + 
                 "Test 1: " + test1 + "\n" +
                 "Test 2: " + test2 + "\n" +
                 "Test 3: " + test3 + "\n" +
                 "Average: " + getAverage();
    return str;
}

}

技术上正确但对不熟悉 Java bean 的人无用的最短可能答案:Studentp194 没有 score 属性。因此,不存在的 score 属性 不会被 XMLEncoder.

编码为 XML

要真正了解发生了什么,如果您想继续使用 XMLEncoder,这可能是也可能不是您要解决的实际问题的最佳解决方案,您必须继续阅读惊人的古老但(恕我直言)很棒 Java Beans specification.

对于任意 class 有一个 Java Beans 属性 命名,比如说,foo,其类型是 Foo,那么它必须有一个 public Foo getFoo() 方法和一个 public void setFoo(Foo foo) 方法。如您所见,您的 Studentp194 class 没有 public int getScore() 方法,也没有 public void setScore(int score) 方法。

(当然,Java Beans 规范实际上并不强制要求这些是您的方法所需的名称。您可以通过多种方式自定义它们,包括使用 BeanInfo classes ,但这远远超出了这个问题的范围。)

回到您的问题和您的代码,XMLEncoder 不会帮助您按原样进行 class 设计,因为您的 getScore 方法需要参数,而您的 setScore 方法接受多个参数。因此它们不是 Java Beans 属性;因此 XMLEncoder 不会对它们进行编码。

假设您出于某种原因想要继续使用 XMLEncoder。然后,您必须重构 class 以符合 Java Beans 规范。我将把它留作 reader 的练习。 :-)

我猜这是某种作业,使用 XMLEncoder 是作业的要求。如果我错了,请查看用于存储和检索信息的不同文件格式。

如果我是对的,那么您想看看您的 Studentp194 class 在逻辑上与 集合 或数组关联的事实分数。您可能希望实际创建一个 class 来表示测试分数,并将此类测试分数的集合存储在您的 Studentp194 class 中。更妙的是,也许您的 Studentp194 class 想要与另一个 class 相关联,例如 Transcript 或类似的可以正确存储此信息的东西。有关这种分解问题的分解方法的更多信息,请阅读 "third normal form" 并从那里开始。我希望这对您有所帮助,欢迎来到 Whosebug。