ValueRequired exception When using the Simple-XML-Framework

I am trying to use the Simple-XML-framework. But I get a ValueRequiredException

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=false, name=, required=true, type=void) on field 'Employees' public java.util.List com.example.xmlparsing.Company.Employees for class com.example.xmlparsing.Company at line 2

My XML file has the following content:

<?xml version="1.0" encoding="utf-8"?>
<Company>
<Emp>
    <Name>Venkat</Name>
    <ID>661511</ID>  
</Emp>
<Emp>
    <Name>Shiv</Name>
    <ID>661311</ID> 
</Emp>
</Company>

My annotation classes are as follows:

Company .java:

package com.example.xmlparsing;

import java.util.List;

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

@Root
public class Company {

@ElementList
public List<Emp> Employees;

}

Emp.java:

package com.example.xmlparsing;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;

@Element(name="Emp")
public class Emp {

@Element
public String Name;

@Element
public String ID;


}

What could be the problem? how to fix it?

+5
source share
1 answer

There are two things in the code:

First you need @Root()instead @Elementin the class Emp:

@Root(name="Emp")
public class Emp {

@Element
public String Name;

@Element
public String ID;


}

Secondly, if you use a list this way:

@Root
public class Company {

@ElementList
public List<Emp> Employees;

}

it is wrapped in another xml tag.
You can either change your xml or just use it @ElementList(inline = true).

+9
source

All Articles