1
votes

These are the errors i'm getting. I understand that there is a problem with my schema and that it cant find the student element. But why cant it find the element is beyond me. Ideally i would like this to work so that i can get the students name, grades and courses to calculate the weighted average grade and to sort the students by name. What i would like to know is what am i doing wrong. I followed the tutorials on Simple XML serializers website but i'm stumped on why it doesn't work. Also the line number it gives there does not lead to anything in my code.

Any help is well appreciated.

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)

This is my XML im trying to deserialize.

<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>

My Java classes that should fit the XML schema.

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(){}
}

Students class

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;
    }

Student class

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;
        }
    }

StudentCourse class

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;
}

Course class

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;
    }
}

Deserializer class

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());



    }

}
1

1 Answers

1
votes

Your XML file contains a <uni>, but you're trying to parse it as if it were a single Student:

    Student student = serializer.read(Student.class, result);
    System.out.println(student.getStudentName());

You've already defined an object corresponding to the <uni>, which may contain many zero or many students. Parse with:

    Uni uni = serializer.read(Uni.class, result);

    for (Student student : uni.students.student) {
        System.out.println(student.getStudentName());
    }