Friday, 19 September 2014

Simplexml list doesn't work



I want to read and parse an XML file containing the information of employees using SimpleXML framework. Here is my XML file:



<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee>
<age>29</age>
<name>Pankaj</name>
<gender>Male</gender>
<role>Java Developer</role>
</Employee>
<Employee>
<age>35</age>
<name>Lisa</name>
<gender>Female</gender>
<role>CEO</role>
</Employee>
</Employees>


Here is Employees class:



@Root
public class Employees {

@ElementList(inline = true)
private List<Employee> list;

public List<Employee> getList() {
return list;
}
}


Here is Employee class:



@Root
public class Employee {

@Element
private String name;

@Element
private String gender;

@Element
private int age;

@Element
private String role;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

@Override
public String toString() {
return " Name=" + this.name + " Age="
+ this.age + " Gender=" + this.gender + " Role=" + this.role;
}
}


But when I try to read and parse the XML file an exception occurs. Here is the output:



Exception in thread "main" org.simpleframework.xml.core.ElementException: Element 'Employee' does not have a match in class Employees at line 3

No comments:

Post a Comment