这些是我得到的错误。 我知道我的架构存在问题,并且无法找到学生元素。但为什么它不能发现元素超出我。理想情况下,我希望这样做,以便我可以获得学生的姓名,成绩和课程来计算加权平均成绩,并按名称对学生进行排序。我想知道的是我做错了什么。我按照Simple XML serializers website上的教程进行了操作,但我很难理解为什么它不起作用。它给出的行号也不会导致我的代码中出现任何内容。
非常感谢任何帮助。
Exception in thread "main" org.simpleframework.xml.core.ElementException: Element 'students' does not have a match in class Student at line 2
at org.simpleframework.xml.core.Composite.readElement(Composite.java:527)
at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
at org.simpleframework.xml.core.Composite$Injector.read(Composite.java:1433)
at org.simpleframework.xml.core.Composite.read(Composite.java:201)
at org.simpleframework.xml.core.Composite.read(Composite.java:148)
at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
at org.simpleframework.xml.core.Persister.read(Persister.java:625)
at org.simpleframework.xml.core.Persister.read(Persister.java:606)
at org.simpleframework.xml.core.Persister.read(Persister.java:584)
at org.simpleframework.xml.core.Persister.read(Persister.java:543)
at org.simpleframework.xml.core.Persister.read(Persister.java:521)
at org.simpleframework.xml.core.Persister.read(Persister.java:426)
at Deserializer.main(Deserializer.java:17)
这是我试图反序列化的XML。
<uni>
<students>
<student>
<studentName>Joe</studentName>
<studentCourses>
<course name="XML" grade="2" />
<course name="Java" grade="5" />
</studentCourses>
</student>
<student>
<studentName>Jane</studentName>
<studentCourses>
<course name="XML" grade="3" />
<course name="Java" grade="5" />
</studentCourses>
</student>
</students>
</uni>
我的Java类应该适合XML模式。
Uni class
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="uni")
public class Uni {
@Element(name="students")
public Students students;
public Uni(){}
}
学生班
import java.util.List;
import org.simpleframework.xml.*;
public class Students {
@ElementList(name="student", entry="students", inline=true)
public List<Student> student;
public Students(){}
public List getProperties(){
return student;
}
学生班
import java.util.List;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
//@Element
public class Student {
@Element(name="studentName")
public String studentName;
@ElementList(name="studentCourses")
public List<StudentCourses> studentCourses;
//public Student (){}
public Student(String studentName){
this.studentName = studentName;
}
public String getStudentName(){
return this.studentName;
}
}
学生课程
import java.util.List;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
@Element
public class StudentCourses {
@ElementList(name="studentCourses")
public List<Course> course;
}
课程班
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
public class Course {
@Element(name="course", required=false)
public String course;
@Attribute(name="name")
public String courseName;
@Attribute(name="grade")
public int grade;
//default constructor
public Course () {}
public String getCourseName (String courseName){
return this.courseName = courseName;
}
public int getCourseGrade (int courseGrade){
return this.grade = courseGrade;
}
}
反序列化程序类
import java.io.File;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class Deserializer {
public static void main(String[] args) throws Exception {
Serializer serializer = new Persister();
File result = new File("stud.xml");
Student student = serializer.read(Student.class, result);
//Students stud = serializer.read(Students.class, result);
System.out.println(student.getStudentName());
//System.out.println(stud.getProperties());
}
}
答案 0 :(得分:1)
您的XML文件包含<uni>
,但您尝试将其解析为单个Student
:
Student student = serializer.read(Student.class, result);
System.out.println(student.getStudentName());
您已经定义了与<uni>
对应的对象,该对象可能包含许多零或多个学生。解析:
Uni uni = serializer.read(Uni.class, result);
for (Student student : uni.students.student) {
System.out.println(student.getStudentName());
}