XML : JAXB unmarshalling exception

The problem is that, when i am trying to unmarshall a simple XML using JAXB i get this exception : javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"students"). Expected elements are <{}student> Can someone help me how to resolve this problem.

This is my XML :

  <students>     <student id="12">        <name>Pesho</name>        <age>23</age>     </student>     <student id="14">       <name>Pesho2</name>       <age>42</age>     </student>  </students>    

This is Student.class :

  @XmlRootElement(name = "student")  @XmlAccessorType (XmlAccessType.FIELD)  public class Student{    String name;  int age;  @XmlAttribute  int id;    setter, getters..  }    

This is the Students.class :

  @XmlRootElement(name = "students")  @XmlAccessorType (XmlAccessType.FIELD)  public class Students {    @XmlElement(name = "student")  private List<Student> students = null;    public List<Student> getStudents() {      return students;  }    public void setStudents(List<Student> students) {      this.students = students;  }}    

And here is the unmarshalling code:

  File file = new File("file.xml");          JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();          Students students = (Students) jaxbUnmarshaller.unmarshal(file);            for (Student student : students.getStudents()){              System.out.println(student.getId());              System.out.println(student.getName());              System.out.println(student.getAge());          }    

No comments:

Post a Comment