简体   繁体   中英

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

You made mistake in defining the name of the list:

@Root
public class Employees {

    @ElementList(inline = true)
    private List<Employee> Employees; //The list name should match with xml list name."

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

In your XML, you have an "Employees" list of elements type "Employee". But in your java program, you defined the "Employees" list as "list":

I found the problem. I must use this:

@ElementList(inline = true, entry="Employee")

Because without this the framework thinks that elements class name is "Entry".

You may try to add type for the list you are using in Employees object. But better also add code how you read your xml file (I have not enough reputation to ask you in comments for now)

@Root
class Employees {

    @ElementList(inline = true, type = Employee.class)
    private List<Employee> list;

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

For your first example in the question, before you add

@ElementList(entry = "Employee")
private List<Employee> list;

the expected XML file was below (and @ElementList(type = Employee.class), I removed inline = true)

<?xml version="1.0" encoding="UTF-8"?>
    <employees>
    <list>
        <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>
    </list>
    </employees>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM