Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList()

I'm trying to get books xml data from URL by using Retrofit. But when app runs, it shows error. I am new so please help me. Here is the error msg:

    02-20 23:06:37.943 23835-23835/com.santossingh.reader E/error:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=work, required=true, type=void) on field 'works' public java.util.List com.santossingh.reader.AWS.GoodReadsModels.Results.works for class com.santossingh.reader.AWS.GoodReadsModels.Results at line 2

Here is the XML file structure and my models and classes:

1- The Structure of URL XML file :

<?xml version="1.0" encoding="UTF-8"?>
        <GoodreadsResponse>
          <Request>
            <authentication>true</authentication>
              <key><![CDATA[xxxxx]]></key>
            <method><![CDATA[search_index]]></method>
          </Request>
          <search>
          <query><![CDATA[business]]></query>
            <results-start>1</results-start>
            <results-end>20</results-end>
            <total-results>109755</total-results>
            <source>Goodreads</source>
            <query-time-seconds>0.22</query-time-seconds>
            <results>
                <work>
          <id type="integer">17624817</id>
          <books_count type="integer">85</books_count>
          <ratings_count type="integer">156992</ratings_count>
          <text_reviews_count type="integer">8489</text_reviews_count>
          <original_publication_year type="integer">2011</original_publication_year>
          <original_publication_month type="integer" nil="true"/>
          <original_publication_day type="integer" nil="true"/>
          <average_rating>4.01</average_rating>
          <best_book type="Book">
            <id type="integer">12609433</id>
            <title>The Power of Habit: Why We Do What We Do in Life and Business</title>
            <author>
              <id type="integer">5201530</id>
              <name>Charles Duhigg</name>
            </author>
            <image_url>https://images.gr-assets.com/books/1366758683m/12609433.jpg</image_url>
            <small_image_url>https://images.gr-assets.com/books/1366758683s/12609433.jpg</small_image_url>
          </best_book>
        </work>
      <results>

Here is the clarity of structure <imagelink> of structure XML file for more clarity.

2- Model of Results.class:

@Root (name = "results", strict = false)
public class Results {

    @ElementList (name = "work", inline = true)
    public List<Work> works;

    public Results() {
    }

    public Results(List<Work> works) {
        this.works = works;
    }

    public List<Work> getWorks() {
        return works;
    }

    public void setWorks(List<Work> works) {
        this.works = works;
    }
}

2- Model of Work.class:

@Root(name = "work", strict = false)
public class Work {

    @Path("best_book")
    @Element(name = "id")
    private int id;

    @Path("best_book")
    @Element (name = "title")
    private String title;

    @Path("best_book/author")
    @Element(name = "name")
    private String author;

    @Path("best_book")
    @Element (name = "image_url")
    private String image;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}
like image 761
Santosh Singh Avatar asked Feb 21 '17 04:02

Santosh Singh


2 Answers

I found the answer. In the Result.class just change the @ElementList() parameter. Here is the solution:

Error -

    @ElementList(name = "work", inline = true)
    public List<Work> works;

Solution -

    @ElementList(inline = true, required = false)
    public List<Work> works;

Whenever that error occured just remember two things in @ElementList()

1- inline=true
2- required=false
example : @ElementList(inline = true, required = false)
like image 121
Santosh Singh Avatar answered Sep 27 '22 21:09

Santosh Singh


I have faced this issue too. Looking at the documentation, it said as below.

The ValueRequiredException is thrown when an attribute or element is missing from the XML document. This is thrown only if the attribute or element is required according to the annotation for that field within the XML schema class.

Basically this occurs when there is a mismatch between the formed request and your XML request schema.

So, use the annotation

@ElementList(name="XMLNode", required = false)
public <<DataType>> XMLNode;

When required = false the builder will ignore the tag and build the remaining nodes of the request.

P.S: This is mainly useful when you are using the same class to form the XML request as well as parse the response.

like image 43
Akhil Ghatiki Avatar answered Sep 27 '22 21:09

Akhil Ghatiki