Getting attribute values from a self-closing element using Simple XMLframework and java



I'm trying to get the attribute values of the course element(grades="" & weight=""). As i understand the situation the course names and grades are already mapped to that specific person. What i would like to get out of this is a list of students, grades and weight of grade for each individual (ie. studentName="Joe" + joesGrades="2,5,4" + weightOfcourse="2,4,3").


And after i have the data mapped i would like to calculate the individual weighted average grade of the student. And thats my second question. How do i do that !? It's really beyond me. How do i link together 3 different kinds of lists ?


I'm guessing it should be a two dimensional array or something similar ?


This is what i have so far:



for (Student student : uni.students.student) {

System.out.println(student.getStudentName() + student.studentCourses.getAllStudentCourses());
}


Outputs :


Joe[Course [courseName=XML, weight=2, grade=2], Course [courseName=Java, weight=4, grade=5], Course [courseName=SQL, weight=3, grade=4]]


Jack[Course [courseName=XML, weight=2, grade=2], Course [courseName=Java, weight=4, grade=5], Course [courseName=SQL, weight=3, grade=4]]


Jane[Course [courseName=XML, weight=2, grade=2], Course [courseName=Java, weight=4, grade=5], Course [courseName=SQL, weight=3, grade=4]]


The XML that i deserialize:



<uni>
<students>

<student>
<studentName>Joe</studentName>
<studentCourses>
<course name="XML" weight="2" grade="2" />
<course name="Java" weight="4" grade="5" />
<course name="SQL" weight="3" grade="4" />
</studentCourses>
</student>

<student>
<studentName>Jack</studentName>
<studentCourses>
<course name="XML" weight="2" grade="2" />
<course name="Java" weight="4" grade="5" />
<course name="SQL" weight="3" grade="4" />
</studentCourses>
</student>

<student>
<studentName>Jane</studentName>
<studentCourses>
<course name="XML" weight="2" grade="2" />
<course name="Java" weight="4" grade="5" />
<course name="SQL" weight="3" grade="4" />
</studentCourses>
</student>

</students>
</uni>


StudentCourses class



import java.util.List;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;



@Root(name="studentCourses", strict=false)
public class StudentCourses {

@ElementList(name="course", inline=true)
public List<Course> course;

public StudentCourses(){}

public List<Course> getAllStudentCourses(){
return course;

}


}


Course class



import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;

@Element(name="course")
public class Course {

@Attribute(name="name", required=false)
public String courseName;

@Attribute(name="weight")
public int weight;

@Attribute(name="grade")
public int grade;

public Course () {}

public String getCourseNames (){
return courseName;
}

public int getCourseGrades (){
return grade;
}

@Override
public String toString() {
return "Course [courseName=" + courseName + ", weight=" + weight
+ ", grade=" + grade + "]";
}
}


Deserializer



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 studentsXML = new File("stud.xml");

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


for (Student student : uni.students.student) {

System.out.println(student.getStudentName() + student.studentCourses.getAllStudentCourses());
}



}

}

No comments:

Post a Comment