JavaXB:如何使列表成为xml文件的属性

时间:2016-04-02 01:06:15

标签: java xml

我有一份学生名单中的成绩列表。我想将其保存到xml文件中。任何人都知道如何获得这样的xml文件,是否可以使用JavaXB?

<students>
        <student>
            <firstName>Hans</firstName>
            <lastName>Muster</lastName>
            <grades>
                   <grade>
                         <value>10</value>
                   </grade>
                   <grade>
                         <value>9</value>
                   </grade>
                   <grade>
                         <value>8</value>
                   </grade>
                   <grade>
                         <value>10</value>
                   </grade>
        </student>
        <student>
            <firstName>Ruth</firstName>
            <lastName>Mueller</lastName>
            <grades>
                   <grade>
                         <value>7</value>
                   </grade>
                   <grade>
                         <value>4</value>
                   </grade>
                   <grade>
                         <value>8</value>
                   </grade>
                   <grade>
                         <value>11</value>
                   </grade>
        </student>
    </students>

这是我的班级:

public class Student {
    private final StringProperty firstName;
    private final StringProperty lastName;
    private final ListProperty<Integer> grades;
    public Client() {
        ObservableList<Integer> gradess = FXCollections.observableArrayList();
        grades=new SimpleListProperty<Integer>(gradess);
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);
}
}

这是我的ClassWrapper

@XmlRootElement(name = "students")
public class StudentWrapper {

    private List<Student> students;

    @XmlElement(name = "students")
    public List<Student> getStudents() {
        return clients;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }
}

0 个答案:

没有答案