I am trying to use Simple-XML-framework. But i am getting 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?
There are two things wrong in your Code:
First, you need @Root()
instead of @Element
at Emp
class:
@Root(name="Emp")
public class Emp {
@Element
public String Name;
@Element
public String ID;
}
Second, if you use a list that way:
@Root
public class Company {
@ElementList
public List<Emp> Employees;
}
its wrapped into another xml tag.
You can either change your xml or simply use @ElementList(inline = true)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With